java - 让 WebDriver 在没有 ExpectedCondition 的情况下等待

标签 java selenium firefox selenium-webdriver

Java/Selenium WebDriver/火狐

There is a text 'Input' field on a page. Then there is a 'Submit' button below it. Both the input field as well as the Submit button are already enabled when the page is loaded. After entering text in the input field, is there a way to make the WebDriver wait for 'x' seconds before clicking on Submit button, instead of clicking it right away.

The option I can see is Thread.sleep(x) which I understand is not an efficient way. The other option is using new WebDriverWait(driver,'x') without the Expected Conditions (as there is no Expected condition here as the Submit button is already visible and clickable). In this context, is this the same as using Thread.sleep(x)? Is there any other option?

最佳答案

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

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

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

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

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

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

关于java - 让 WebDriver 在没有 ExpectedCondition 的情况下等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35903190/

相关文章:

java - 将 ViewPort 添加到 JPanel

java - 按空格分隔字符串

javascript - javascriptExecutor 执行后无法执行处理步骤

javascript - 手动删除值后无法通过 JS 设置输入值(Chrome 和 FF)

javascript - Firefox 没有从 JavaScript 设置 css 样式?

Javascript 自动完成功能适用于 IE 和 Chrome,但不适用于 Firefox

java - 如何在这里获取 "What' s?”来自纬度和经度的信息?

java - 如何使用 Maven 使可执行 jar 在一段时间内选择属性文件

python - 如何防止 Selenium 在每个函数后关闭浏览器?

java - 防止 Selenium WebDriver 在捕获异常后退出