在我的时区下午好。
我开始使用 Selenium 来测试我的 Web 应用程序。我正在将 WebDriver API 与 IEDriverServer.exe 结合使用。操作系统 -> Windows XP 主要问题是测试不稳定。有时运行,有时抛出异常。 例如,这是测试不稳定的常见地方。 我必须打开一个新窗口并开始填写一些字段。
driver.findElement(By.xpath("//input[@name='"+button+"' and @type='button']")).click();//BUTTON THAT OPENS THE NEW WINDOW
long initDate = System.currentTimeMillis();
while(driver.getWindowHandles().size() <= numberPopUps){
Thread.sleep(500);
//15 seconds waiting for the pop-up
if((System.currentTimeMillis() - initDate) > 15000){
throw new Exception("Timeout to open popup");
}
}
for(String winHandle : driver.getWindowHandles()){
if(!winHandle.equals(mWindow)){
driver.switchTo().window(winHandle);
break;
}
}
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);//WAIT THAT THE PAGE COMPLETELY LOAD
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='descricaoMov']")));//VERIFY IF THIS INPUT IS ON THE DOM
`driver.findElement(By.xpath("//input[@name='descricaoMov']")).sendKeys("TESTE SELENIUM");`//This is where sometimes the test throws exception saying that is unable to find this element
,我想问一下,这怎么可能?
提前致谢 最好的问候
最佳答案
您在这里重复了您的努力。 wait.until 行与下一行的操作完全相同,但 .sendKeys() 除外。试试这个:
WebElement descriaoMov = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='descricaoMov']")));
descriaoMov.sendKeys("TEST SELENIUM");
此外,CSS 选择器比 XPath 更擅长查找元素。我建议将上面的 xpath 部分更改为:
By.cssSelector("input[name='descriaoMov']")
关于java - 不稳定的测试 Selenium ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22099124/