We often encounter the use of selenium to manipulate an element on the page, we need to wait for the page to be loaded after the completion of the operation. Otherwise, the element on the page does not exist and throws an exception.
Or the Ajax asynchronous loading, we need to wait for the element to be loaded before we can manipulate
Selenium provides a very simple, intelligent way to determine whether an element exists.
Read Catalogue
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.
<HTML> <Head> <title>Set Timeout</title> <style>. Red_box{Background-color:Red;width = 20%; height:100px;Border:None;} </style> <Script> functionShow_div () {setTimeout ("Create_div ()", the); } functionCreate_div () {D=Document.createelement ('Div'); D.classname= "Red_box"; Document.body.appendChild (d); } </Script> </Head> <Body> <ButtonID= "B"onclick= "Show_div ()">Click</Button> </Body></HTML>
Implicit wait
New firefoxdriver (); Driver.get ("file:///C:/Users/Tank/Desktop/set_timeout.html"); Driver.manage (). Timeouts (). implicitlywait (timeunit.seconds); = 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