Java Selenium Smart Wait page load complete sample code _ Practical Tips

Source: Internet
Author: User

Java Selenium Smart Wait page load complete

We often encounter the use of selenium to manipulate an element on the page, you need to wait for the page to be completed after loading to operate. Otherwise, the element on the page does not exist, and an exception is thrown.

or encounter Ajax asynchronous loading, we need to wait for the element load to complete before the operation

The selenium provides a very simple, intelligent way to determine whether an element exists.

Reading Table of Contents

    1. Instance requirements
    2. Implicit wait
    3. Explicit wait

Instance requirements

Example: set_timeout.html The following HTML code, click the Click button for 5 seconds, the page will appear a red div quickly, we need to write an automated script to determine whether the div exists, and then put this div then highlighted.

 
 

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"));  
  

which

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

It means to wait 10 seconds, if the element does not exist after 10 seconds, it throws an exception org.openqa.selenium.NoSuchElementException

Explicit wait

Explicitly waiting to use the expectedconditions in the class, you can make a trial wait for the judgment.

Explicit wait conditions can be customized for a more complex page-waiting condition

Conditions of waiting

Webdriver method

Page elements are available on the page and can be clicked

Elementtobeclickable (by Locator)

page element is in the selected state

Elementtobeselected (webelement Element)

Page elements exist on the page

Presenceofelementlocated (by Locator)

Whether specific text is included in the page element

Texttobepresentinelement (by Locator)

Page element value

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

Titles (title)

Titlecontains (java.lang.String title)

The test code will continue to perform the subsequent test logic backwards only if the condition that satisfies the explicit wait is satisfied

The test program throws an exception if the set maximum explicit wait time threshold is exceeded.

public static void TestWait2 (Webdriver driver)
 {
  driver.get ("e:\\stashfolder\\huoli_28@hotmail.com\\stash\ \tank-moneyproject\\ Pudong Software Park training center \ My textbook \\Selenium webdriver\\set_timeout.html "); 
  
  webdriverwait wait = new webdriverwait (driver);
  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); 
 }

The above is the Java selenium waiting for the page loading data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.