Selenium FluentWait方法无法忽略NoSuchElement

时间:2015-03-26 15:14:49

标签: java selenium webdriver

我有一个网络报告页面,我必须验证每个页面中的所有日期字段。我可以访问每个页面并进行验证,但我的脚本在FluentWait方法中出错

enter image description here

我必须获取第一页的日期,验证它,然后单击“下一步”按钮(如果已启用)。如果没有启用,我就会退出循环。

do {

......
.....
FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(500, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);

    fluentWait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00"))));
    driver.findElement(By.id("ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00")).click();


} while (driver.findElement(By.id("ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00")).isEnabled());

但是我收到了一个错误。为什么NoSuchElement覆盖不起作用?如何优雅地退出 ExpectedConditions.elementToBeClickable

有更好的方法吗?

org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for element to be clickable: [[FirefoxDriver: firefox on XP (256df980-a068-474c-9469-2ff4e8e13b3e)] -> id: ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00]
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'PC-L-JOSU', ip: '172.26.70.109', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_40'
Driver info: driver.version: unknown
org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for element to be clickable: [[FirefoxDriver: firefox on XP (256df980-a068-474c-9469-2ff4e8e13b3e)] -> id: ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00]
 org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:259)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
at com.stta.ReportsParkingTransactions.ReportsParkingTransactions.ReportParkingTransactions(ReportsParkingTransactions.java:414)

由于

修改


我试过没有预期的条件,看到这个错误。我缺少什么

enter image description here

有人请帮忙! ..: - )...我用谷歌搜索了这个并且看起来无处不在,我在最后几天都被卡住了!

2 个答案:

答案 0 :(得分:0)

我只能说,你不应该ExpectedConditions使用FluentWait,至少你不需要。根据{{​​3}},以下内容应该足够了

// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

WebElement reportViewer = wait.until(new Function<WebDriver, WebElement>() {
  public WebElement apply(WebDriver driver) {
    return driver.findElement(By.id("ReportViewer1_ctl06_ctl00_Next_ctl00_ctl00"));
  }
});

reportViewer .click();

答案 1 :(得分:0)

尝试在忽略超时异常的方法中添加'TimeoutException.class'。因此测试用例不会因超时异常而失败。

EX: .ignoring(NoSuchElementException.class,TimeoutException.class)