【问题标题】:Can a condition be defined on timeout for WebDriverWait?可以为 WebDriverWait 定义超时条件吗?
【发布时间】:2014-11-06 15:57:02
【问题描述】:

是否可以在时间单位到期(WebDriverWait 超时)时包含条件 - 单击按钮/driver.navigate.back() 的示例?

示例: 对于以下陈述 - WebDriverWait wait60 = new WebDriverWait(driver, 60); wait60.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.locator)));

如果在 60 秒内未在网页上加载要点击的元素 - 我希望执行 driver.navigate.back() 语句。任何定义此类语句的方法(例如在类级别),以便所有 wait60.until 条件在超时时导致相同的定义语句?

【问题讨论】:

  • 你不想用try catch?

标签: selenium-webdriver webdriver


【解决方案1】:

我认为这不包含在WebDriverWait 功能中。我可能会使用try-catch异常处理,当你捕捉到timeoutException,然后调用driver.navigate.back()

您可以创建一个可以执行此操作的方法并随时调用它。

【讨论】:

    【解决方案2】:

    像这样把它放在一个 try-catch 块中:

    try{
        WebDriverWait wait60 = new WebDriverWait(driver, 60);
        wait60.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.locator)));
    }catch(Exception e){
        System.out.println("Element not found in due time.  "+e.getMessage);
        driver.navigate().back();
    }
    

    如果没有及时找到该元素,将抛出 TimeoutException 并随后在 catch 块中进行处理。此外,之后浏览器被导航回来。

    【讨论】:

    • 将抛出 TimeoutException 而不是 NoSuchElementExeption。我已经编辑了这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多