Several wait ways in selenium, pay special attention to the usage of implicitlywait

Source: Internet
Author: User

Transfer from Http://blog.csdn.net/pf20050904/article/details/20052485?utm_source=tuicool

Recently, when using selenium to determine whether an element exists in the process of the project, I encountered a very big problem, and the following methods were used to wait for a long time each time, because there was not enough knowledge about the selenium implementation method to find the solution.

Private Boolean iselementpresent (by) {
try {

Driver.findelement (by);
return true;
} catch (Nosuchelementexception e) {
return false;
}
}

Unknown so, after tracing the source to find the start driver when the use of Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);  The wait time set here is for the global setting, and the timeout for executing all commands in Webdriver is set to 30 seconds, such as the Findelement method above, the element will wait for 30 seconds by default.  Sometimes it's just a matter of simply judging if an element exists and executing it immediately, and the settings here cause the script to execute slowly. The waiting time in the selenium will be put out to everyone Zhang Zhang experience.

Selenium delay wait is divided into explicit wait (Explicit wait) & implicit wait (implicit wait).

1. Wait for an explicit

Explicit wait, is clear to wait for an element to appear or is an element of the clickable conditions, etc., and so on, and so on, unless not found within the specified time, then jump out of exception.

Such as:

1 newWebDriverWait(driver, 15).until(
2     ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator"))
3 );

Here, 15 is the number of seconds to wait. If the condition in the until () method is not met, it will always wait here for 15 seconds, still cannot find, throws an exception.

You can also write this:

01 WebDriver driver = newFirefoxDriver();
02 driver.get( www.baidu.com);
03 WebElement e = (newWebDriverWait( driver, 10)) .until(
04     newExpectedCondition< WebElement>(){
05         @Override
06         publicWebElement apply( WebDriver d) {
07             returnd.findElement( By.id("id locator"));
08         }
09     }
10 );

This webelement is directly obtained through the callback function. That is, the page element.

If just want to judge whether the page is loaded into a place, you can use the first method; But if you need to get a webelement, either way, just the first way you need to get the action one more step.

1 Wait w = newWait(){
2     @Override
3     publicbooleanuntil() {
4         returnwebElement.isDisplayed();
5     }
6 };

In addition, this way of waiting, in the loading JS code when the judgment will be more convenient, anyway I only in the code so seen, but no use, interested friends can study.

2. Implicit wait

1 driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);

Implicit wait, the implicit wait here is for driver each execution of the command of the longest execution time can also be understood as time-out, some people misunderstand here, that is let driver wait a while, indeed some time can let driver wait a while, but the impact is global, each driver execution   Cannot find the element will wait for the time set here, it is assumed that somewhere this value is set too long, must be restored after the completion of execution, or if an element exists, you will encounter a very big problem. The improved method is as follows:

Webdriver will have an implicit wait, but the parameters are only time, which leads me to what elements I need to appear, and I don't necessarily have to wait for it,

Private Boolean iselementpresent (by) {
try {
Driver.manage (). Timeouts (). implicitlywait (1, timeunit.seconds);
Driver.findelement (by);
return true;
} catch (Nosuchelementexception e) {
return false;
}
} cannot get an element, we delay ...

3. Thread hibernation

1 Thread.sleep()

Several wait ways in selenium, pay special attention to the usage of implicitlywait

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.