线程“main”中的exceptionorg.openqa.selenium.NoSuchElementException:无法找到element:// *

我不得不重新测试xpath ,以前它工作正常,但现在它给了我一个错误。

我尝试过不同的定位器,比如idname 。 但仍然得到相同的错误。

 package staging; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class login { public static void main (String[]args){ System.setProperty("webdriver.gecko.driver","C:\\Program Files\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //opening the browser driver.get("https://staging.keela.co/login"); //logging driver.findElement(By.xpath("//*[@id='login-email']")).sendKeys("bandanakeela@yopmail.com"); driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela"); driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click(); } } 

当您访问URL https://staging.keela.co/login有一个Ajax加载器会阻止UI,因此我们必须等待Ajax加载程序完成加载所有WebElements,并且emailpassword字段变为可见。 为实现这一目标,我们将引入ExplicitWaitWebDriverWait其中ExpectedConditions设置为email字段的elementToBeClickable 。这是工作代码块:

 System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://staging.keela.co/login"); WebDriverWait wait = new WebDriverWait (driver, 15); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='login-email']"))); element.sendKeys("bandanakeela@yopmail.com"); driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys("keela"); driver.findElement(By.xpath("//button[@class='btn btn-sm btn-block btn-primary']")).click(); 

试试以下代码。

注意:如果id属性可用,那么你应该使用idxpath尝试使用相对xpath

我使用了显式等待方法,因此您的驱动程序可以在页面完全加载后找到下一个webelement

 driver.get("https://staging.keela.co/login"); driver.manage().window().maximize(); //Explicit wait for 60 seconds, to find the webelement. You can increase or decrease the time as per your specification. new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("login-email")))); driver.findElement(By.id("login-email")).sendKeys("bandanakeela@yopmail.com"); driver.findElement(By.id("login-password")).sendKeys("keela"); driver.findElement(By.xpath("//button[@type='submit'][text()='Log in']")).click(); 

该页面使用js来构造元素。 所以我建议你使用phantomjs驱动程序。

然后你必须等到元素存在。 页面加载时,您会看到齿轮图标。 等到元素加载。 而且你也可以使用id而不是xpath,因为你知道你的元素id。

您可以选择要使用的等待类型。 显式等待或隐式等待。

这是selenium文档。

以及wait的示例代码:

 WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("login-email"))); 

或者你可以等到页面加载:

 new WebDriverWait(firefoxDriver, pageLoadTimeout).until( webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete")); 

您正在打开URL,并在下一刻输入email-id。 在输入email-id之前,您需要检查页面是否已满载。 在这种情况下,明确的等待会帮助你 –

 //opening the browser driver.get("https://staging.keela.co/login"); //Explicit wait WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20); WebElement email; email = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-email"))); //logging driver.findElement(By.xpath("//*[@id='login-email']")).sendKeys("bandanakeela@yopmail.com"); driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela"); driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click();