Java 硒中是否隐式等待,仅用于元素搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29591084/
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
Does implicit wait in selenium, only used for element search?
提问by mahan07
I wanted to understand the usage of implicit wait in selenium.
我想了解 selenium 中隐式等待的用法。
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
Will it be used only for element search or for any other purpose like for page load and refresh? As usual, I am using this in the beginning of driver initialization.
它是仅用于元素搜索还是用于页面加载和刷新等任何其他目的?像往常一样,我在驱动程序初始化的开始使用它。
But in my application I want to inspect error message after entering the wrong password, but as soon as I enter the wrong password, it will leave the page and will not wait for the error message on the same page. Will it initialize/load the page only once?. When I use Thread.sleep(3) it stops at the page for 3 secs and also properly reads the error message.
但是在我的应用程序中,我想在输入错误密码后检查错误消息,但是一旦我输入错误密码,它就会离开页面并且不会等待同一页面上的错误消息。它只会初始化/加载页面一次吗?当我使用 Thread.sleep(3) 时,它会在页面上停止 3 秒,并且还会正确读取错误消息。
But I don't want to use Thread.sleep, since I am already using implicit wait.
但我不想使用 Thread.sleep,因为我已经在使用隐式等待。
Can anyone please tell me its use and how do I resolve this error?
谁能告诉我它的用途以及如何解决此错误?
Code Snippet below:(Not Working,returning FAILURE) (Without Thread.sleep)
下面的代码片段:(不工作,返回失败)(没有 Thread.sleep)
setText(WebElements.TEXT_BOX, PASSWORD);
click(WebElements.SUBMIT_BUTTON);
//Thread.sleep(3000);
if (isElementPresent(WebElements.ERROR_MESSAGE)) {
return SUCCESS;
}
else
return FAILURE;
采纳答案by Saifur
The main difference between the Implicitand Explicitwaits is that the Implicit wait polls the DOMfor a defined amount of time(let's say 10s) and looks for the element whereas Explicit wait periodically sends the request to the server and check for the element in DOM.
隐式等待和显式等待之间的主要区别在于,隐式等待在定义的时间量(假设为 10 秒)内轮询DOM并查找元素,而显式等待则定期向服务器发送请求并检查 DOM 中的元素.
The Implicit wait is firm and fixed to the driver instance. So, once you set that time it is set for the entire driver instance and applied everywhere. With that in place, when you use any command to find an element it polls the DOM for that defined amount of time without talking to the server/sending another requestperiodically and finally throws an exception if nothing found.
隐式等待是固定的并且固定到驱动程序实例。因此,一旦您设置了该时间,它就会为整个驱动程序实例设置并应用于任何地方。有了这个,当你使用任何命令来查找一个元素时,它会在定义的时间内轮询 DOM,而不与服务器对话/定期发送另一个请求,如果没有找到,最终抛出异常。
On the other hand, Explicitwait has a default timeof 500ms sleeps in it unless you explicitly override that. Meaning it acts as Thread.sleep();
in between the intervals and sleeps for 500ms and after that sends the request to the server and periodically checks for the element and conditions you defined with a polling interval defined and reaches the timeout.
另一方面,显式等待的默认时间为 500 毫秒,除非您明确覆盖它。这意味着它Thread.sleep();
在间隔之间起作用并休眠 500 毫秒,然后将请求发送到服务器并定期检查您定义的元素和条件,并定义了轮询间隔并达到超时。
So, in your case the Thread.Sleep()
is working as Explicitwait. The proper resolutions is of course to use Explicitwait and periodically checks for the element and it's expected condition.
因此,在您的情况下,Thread.Sleep()
它作为显式等待工作。正确的解决方法当然是使用显式等待并定期检查元素及其预期条件。
setText(WebElements.TEXT_BOX, PASSWORD);
WebDriverWait wait = new WebDriverWait(driver,10);
try{
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id of the element to be located")));
return SUCCESS;
}
catch (NoSuchElementException exception)
{
return FAILURE;
}
回答by mentallurg
First you do some click. Then teh page changes. Before you call isElementPresent(), how will you know, if the page has already loaded?
首先你点击一下。然后页面发生变化。在调用 isElementPresent() 之前,如何知道页面是否已经加载?
Better would be not to use implicitlyWait() and not sleep(). Use FluentWait: https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html
最好不要使用implicitlyWait() 而不是sleep()。使用 FluentWait:https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html
FluentWait periodically checks, if your condition is "true". If not (e.g. if the page is not yet loaded completely), waits some time and checks again.
FluentWait 会定期检查您的条件是否为“真”。如果没有(例如,如果页面尚未完全加载),请等待一段时间并再次检查。
回答by testing
I will suggest you to use explicit wait as given below
我建议您使用如下所示的显式等待
int wait = 10;
WebDriverWait WAIT = new WebDriverWait(driver,wait);
try{
WAIT.until(ExpectedConditions.presenceOfElementLocated(By.id("id of element")));
return SUCCESS;
}
catch (NoSuchElementException exception)
{
return FAILURE;
}
difference between Difference Between Implicit, Explicit & Fluent Wait you can find here
隐式、显式和流利之间的区别等等你可以在这里找到