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/