【问题标题】:Making the WebDriver wait without a ExpectedCondition让 WebDriver 在没有 ExpectedCondition 的情况下等待
【发布时间】:2016-06-24 12:17:57
【问题描述】:

Java / Selenium WebDriver / Firefox

页面上有一个文本“输入”字段。然后在它下面有一个“提交”按钮。 加载页面时,输入字段和提交按钮都已启用。 在输入字段中输入文本后,有没有办法让 WebDriver 在单击提交按钮之前等待“x”秒,而不是立即单击它。

我可以看到的选项是 Thread.sleep(x) 我理解这不是一种有效的方法。 另一个选项是在没有预期条件的情况下使用 new WebDriverWait(driver,'x') (因为这里没有预期条件,因为提交按钮已经可见且可单击)。在这种情况下,这是否与使用 Thread.sleep(x) 相同? 还有其他选择吗?

【问题讨论】:

    标签: java selenium firefox selenium-webdriver


    【解决方案1】:

    Selenium waits 旨在等待特定条件。 Implicit wait 正在等待元素存在于 driver.findElement() 的 DOM 中,explicit wait 正在等待 ExpectedCondition 成为 true

    但是,只要满足条件,代码就会继续执行,如果条件失败,则会抛出异常。

    您可以通过一些操作使代码在没有线程的情况下“休眠”

    WebDriverWait tempWait = new WebDriverWait(driver, 10); // define local/temp wait only for the "sleep"
    try {
        tempWait.until(ExpectedCondition); // condition you are certain won't be true
    }
    catch (TimeoutException) {
        continue; // catch the exception and continue the code
    }
    // continue the code
    

    这将导致代码模拟“睡眠”10 秒(代码不会继续,但tempWait.until 会反复检查是否满足ExpectedCondition)。

    这是一种肮脏的工作,效率甚至低于Thread.sleep()。如果目的是等待一段时间而不考虑网络状况,我建议您使用Thread.sleep()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-22
      • 2020-01-12
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2015-06-22
      • 2013-11-16
      相关资源
      最近更新 更多