Java selenium smart waiting for page loading to complete sample code, selenium sample code
Java selenium smart wait for page loading to complete
When Using selenium to operate an element on a page, you must wait until the page is loaded. Otherwise, an exception is thrown if the element on the page does not exist.
Or when AJAX is asynchronously loaded, we need to wait until the elements are loaded.
Selenium provides a simple and intelligent method to determine whether an element exists.
Reading directory
- Instance requirements
- Implicit wait
- Explicit waiting
Instance requirements
Example: the html code under set_timeout.html. After clicking the click button for 5 seconds, a red div will appear on the page. We need to write an automatic script to intelligently determine whether the div exists, then, highlight the div.
Implicit wait
WebDriver driver = new FirefoxDriver(); driver.get("file:///C:/Users/Tank/Desktop/set_timeout.html"); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); WebElement element = driver.findElement(By.cssSelector(".red_box")); ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);
Where
Driver. manage (). timeouts (). implicitlyWait (10, TimeUnit. SECONDS );
It means that a total of 10 seconds are waiting. If the element does not exist after 10 seconds, an exception org. openqa. selenium. NoSuchElementException will be thrown.
Explicit waiting
Explicitly waiting for the use of ExpectedConditions class self-contained method, you can make an explicit waiting judgment.
Explicit waiting allows you to customize the waiting conditions for more complex page waiting conditions.
Waiting Condition |
WebDriver Method |
Whether the page element is available on the page and can be clicked |
ElementToBeClickable (By locator) |
The page element is selected. |
ElementToBeSelected (WebElement element) |
The page element exists in the page |
PresenceOfElementLocated (By locator) |
Whether the page element contains specific text |
TextToBePresentInElement (By locator) |
Page element value |
TextToBePresentInElementValue (By locator, java. lang. String text) |
Title) |
TitleContains (java. lang. String title) |
The test code continues to run the subsequent test logic only when the explicit waiting conditions are met.
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 \ huoli_28@hotmail.com \ Stash \ Tank-MoneyProject \ Pudong Software Park Training Center \ My teaching material \ Selenium Webdriver \ set_timeout.html "); webDriverWait wait = new WebDriverWait (driver, 20); wait.until(ExpectedConditions.presenceOfElementLocated(By.css Selector (". red_box "); WebElement element = driver.findElement(By.css Selector (". red_box "); (javascriptexecutor)driver.exe .exe cuteScript (" arguments [0]. style. border = \ "5px solid yellow \" ", element );}
The above is to sort out the information for Java selenium waiting for page loading. We will continue to add relevant information in the future. Thank you for your support for this site!