java Selenium webdriver 轮询时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44645586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium webdriver polling time
提问by Randrew Silva
I'm looking forward for a proper explanation about the selenium webdriver polling time in Selenium.
我期待着对 Selenium 中 selenium webdriver 轮询时间的正确解释。
As I know, below wait command will wait for 40 seconds until the specific element get clickable
据我所知,下面的等待命令将等待 40 秒,直到特定元素可点击
public void CreateSalesOrder(){
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(btnNewSalesOrser));
btnNewSalesOrser.click();
}
In the 2nd code snippet I've added "Polling" command.
在第二个代码片段中,我添加了“轮询”命令。
public void CreateSalesOrder(){
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.pollingEvery(2, TimeUnit.SECONDS);
wait.until(ExpectedConditions.elementToBeClickable(btnNewSalesOrser));
btnNewSalesOrser.click();
}
What is the use of polling time ?
轮询时间有什么用?
回答by santhosh kumar
If we didn't mention any polling time, selenium will take the default polling time as 500milli seconds. i.e.., script will check for the excepted condition for the webelement in the web page every 500 milli seconds. Your first code snippet works with this.
如果我们没有提到任何轮询时间,selenium 将默认轮询时间为 500 毫秒。即,脚本将每 500 毫秒检查一次网页中 webelement 的异常条件。您的第一个代码片段适用于此。
We use pollingEveryto override the default polling time. In the below example(your second code snippet), the script checks for the expected condition for every 2 seconds and not for 500 milliseconds.
我们使用pollingEvery来覆盖默认的轮询时间。在下面的示例中(您的第二个代码片段),脚本每 2 秒检查一次预期条件,而不是 500 毫秒。
public void CreateSalesOrder()
{
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.pollingEvery(2, TimeUnit.SECONDS);
wait.until(ExpectedConditions.elementToBeClickable(btnNewSalesOrser));
btnNewSalesOrser.click();
}
This polling frequency may actually help in reducing the CPU overload. Refer this javadoc for more info pollingEvery.
这种轮询频率实际上可能有助于减少 CPU 过载。请参阅此 javadoc 以获取更多信息pollingEvery。
Hope this helps you. Thanks.
希望这对你有帮助。谢谢。
回答by JDC
Using WebDriverWait wait = new WebDriverWait(driver, 40);
the driver will wait a maximum of 40 secondsuntil the condition is fulfilled.
使用WebDriverWait wait = new WebDriverWait(driver, 40);
驱动程序最多等待40 秒,直到条件满足。
Using wait.pollingEvery(2, TimeUnit.SECONDS);
specifies that the driver will do the checks (to see if the condition is fulfilled) every 2 secondsuntil the condition is fulfilled.
Usingwait.pollingEvery(2, TimeUnit.SECONDS);
指定驱动程序将每 2 秒执行一次检查(以查看条件是否满足),直到条件满足。
In sum this means that your driver will do a check every 2 secondsfor a period of 40 seconds.
总之,这意味着您的驱动程序将在 40 秒内每 2 秒进行一次检查。
You could also specify the polling interval as a shortcut in the Constructor:
您还可以在Constructor中将轮询间隔指定为快捷方式:
WebDriverWait wait = new WebDriverWait(driver, 40, TimeUnit.SECONDS.toMillis(2));
回答by Monika
For understanding the explanation, you have to understand the polling time for Explicit Wait.
为了理解解释,您必须了解显式等待的轮询时间。
WebDriverWait wait = new WebDriverWait(driver, 40);
WebDriverWait wait = new WebDriverWait(driver, 40);
This waits up to 40 seconds before throwing a TimeoutException unless it finds the element to return within 40 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully hence the default polling time for ExplicitWait is 500 milliseconds.
在抛出 TimeoutException 之前最多等待 40 秒,除非它找到要在 40 秒内返回的元素。WebDriverWait 默认每 500 毫秒调用一次 ExpectedCondition 直到它成功返回,因此 ExplicitWait 的默认轮询时间是 500 毫秒。
wait.pollingEvery(2, TimeUnit.SECONDS);
wait.pollingEvery(2, TimeUnit.SECONDS);
In this case, the polling time is 2 seconds i.e the Expected condition will not be checked after every 500 milliseconds, it should be checked after 2 seconds until the specific elements get clickable.
在这种情况下,轮询时间为 2 秒,即每 500 毫秒后不会检查预期条件,应在 2 秒后检查,直到特定元素可点击。