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
- Instance requirements
- Implicit wait
- 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!