Selenium: 4 ways the element waits

Source: Internet
Author: User

1. Using Thread.Sleep (), this is the most stupid method, but sometimes it can be used and very useful.

2. Implicit wait, implicit wait is when you want to find the element, and this element does not appear immediately, tell Webdriver to query the DOM for a certain time. The default value is 0, but after the setting, the time will work for the entire life cycle of the Webdriver object instance. Webdriver dr = new Firefoxdriver (); dr.manage (). Timeouts (). implicitlywait (timeunit.seconds);3. Using JavaScriptwebelement element = driver.findelement (By.xpath (test));( (Javascriptexecutor) driver). Executescript ("arguments[0].style.border=" 5px Solid Yellow "", Element);

4. Display wait, recommended use display wait

webdriverwait wait = new Webdriverwait (DR, 10);

Wait.until (expectedconditions.visibilityofelementlocated (by.id ("kw"));

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

(1) Whether the page element is available on the page and can be clicked: Elementtobeclickable (by locator)

(2) the page element is selected: elementtobeselected (webelement Element)

(3) page elements exist in the page: presenceofelementlocated (by locator)

(4) Whether a specific text is included in the page element: Texttobepresentinelement (by locator)

(5) page element value: Texttobepresentinelementvalue (by locator, java.lang.String text)

(6) 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.

webdriverwait wait = new webdriverwait (driver,5);

Wait.until (expectedconditions.presenceofelementlocated (By.cssselector (")));

Selenium: 4 ways the element waits

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.