Selenium delay Wait [Switch]

Source: Internet
Author: User

Latency waits of selenium are divided into explicit waits (explicit waits) and implicit waits (implicit wait ).

1. Explicit waiting

Explicit waiting means explicitly waiting for an element to appear or the clickable condition of an element to wait until it is reached, unless it is not found within the specified time, the exception is thrown out.

For example:

123 new WebDriverWait(driver, 15).until(    ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator")));

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

You can also write as follows:

12345678910 WebDriver driver = new FirefoxDriver();driver.get( http://somedomain/url_that_delays_loading);WebElement e = (new WebDriverWait( driver, 10)) .until(    new ExpectedCondition< WebElement>(){        @Override        public WebElement apply( WebDriver d) {            return d.findElement( By.id("id locator"));        }    });

In this way, the webelement is directly obtained through the callback function, that is, the page element.

If you only want to determine whether the page is loaded to a certain place, you can use the first method. However, if you need to obtain a webelement, either method is acceptable, only the first method requires more operations.

123456 Wait w = new Wait(){    @Override    public boolean until() {        return webElement.isDisplayed();    }};

In addition, this waiting method makes it easier to make judgments when loading js code. I have only seen this in the code, but it has never been used. If you are interested, you can study it.

2. Implicit waiting

Implicit waiting, I think it is unreliable to wait, and I will understand it after reading a piece of code.

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

WebDriver will perform an implicit wait, but the parameter only has time, which leads to the emergence of what elements I need. I may not be able to wait for it, but it will only delay the driver for a while. but we can also encapsulate it in a small way to make it useless:

123456789 public boolean isByElementDisplayed(By by, int time) {    boolean status = false;    if (driver.findElement(by).isDisplayed() == false) {        driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);    } else {        status = true;    }    return status;}

If we don't get a certain element, we will delay it...

Finally, there is one of the most inefficient practices:

1 Thread.sleep()

Not recommended.

For the first article, I 'd like to give you more comments.

 

Selenium delay Wait [Switch]

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.