Java Selenium (13) Smart Wait page loading complete

Source: Internet
Author: User

Instance requirements

Example: set_timeout.html The following HTML code, after clicking the click button for 5 seconds, a red div will appear on the page quickly, we need to write an automated script to determine whether the div exists, and then highlight the Div.

Implicit wait
        Webdriver Driver = new Firefoxdriver ();        Driver.get ("file:///C:/Users/Tank/Desktop/set_timeout.html");                    Driver.manage (). Timeouts (). implicitlywait (timeunit.seconds);        webelement element = Driver.findelement (By.cssselector (". Red_box"));              ((Javascriptexecutor) driver). Executescript ("Arguments[0].style.border = \" 5px solid yellow\ "", Element);  

which

Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);

This means that for a total of 10 seconds, if the element does not exist after 10 seconds, an exception will be thrown org.openqa.selenium.NoSuchElementException

Explicit wait

Explicitly waiting to use the Expectedconditions class in the self-contained method, you can make a trial wait for the judgment.

Explicitly waiting for a condition that can be customized for more complex page-waiting conditions

Condition of waiting

Webdriver method

Whether page elements are available on the page and can be clicked

Elementtobeclickable (by Locator)

The page element is in the selected state

Elementtobeselected (webelement Element)

Page elements exist in the page

Presenceofelementlocated (by Locator)

Whether to include specific text in the page element

Texttobepresentinelement (by Locator)

Page element values

Texttobepresentinelementvalue (by locator, java.lang.String text)

Caption (title)

Titlecontains (java.lang.String title)

The test code will continue to execute the subsequent test logic backwards only if the condition satisfies the explicit wait

If the maximum explicit wait time threshold is exceeded, the test program throws an exception.

    public static void TestWait2 (Webdriver driver)    {        driver.get ("E:\\stashfolder\\[email protected]\\stash\\ tank-moneyproject\\ Pudong Software Park Training center \ \ My textbook \\Selenium webdriver\\set_timeout.html ");                    webdriverwait wait = new webdriverwait (driver, a);        Wait.until (expectedconditions.presenceofelementlocated (By.cssselector (". Red_box"));        webelement element = Driver.findelement (By.cssselector (". Red_box"));              ((Javascriptexecutor) driver). Executescript ("Arguments[0].style.border = \" 5px solid yellow\ "", Element);      }

Java Selenium (13) Smart Wait page loading complete

Related Article

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.