下午好,在我的时区。
我开始使用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.till 行完全执行下一行正在执行的操作,但 .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']")