Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of selenium Intelligent waiting for Page loading in java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the java selenium intelligent waiting page to load the completion of the example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Java selenium intelligently waits for the page to load

We often encounter when using selenium to manipulate an element on a page, we need to wait for the page to load before we can operate. Otherwise, the element on the page does not exist and an exception is thrown.

Or when we encounter AJAX asynchronous loading, we need to wait for the element to be loaded before we can operate.

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

Example requirement

Example: the html code under set_timeout.html, 5 seconds after clicking the click button, a red div will appear on the page. We need to write an automated script intelligently to determine whether the div exists, and then highlight the div.

Set Timeout .red _ box {background-color: red; width = 20%; height: 100px; border: none;} function show_div () {setTimeout ("create_div ()", 5000);} function create_div () {d = document.createElement ('div'); d.className = "red_box"; document.body.appendChild (d);} click

Implicit waiting

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)

Among them

Driver.manage (). Timeouts (). ImplicitlyWait (10, TimeUnit.SECONDS)

It means to wait for a total of 10 seconds. If the element does not exist after 10 seconds, an exception org.openqa.selenium.NoSuchElementException will be thrown.

Explicit wait

Explicit wait can be judged explicitly by using the method included in the ExpectedConditions class.

Explicit wait allows you to customize wait conditions for more complex page wait conditions

Conditions of waiting

WebDriver method

Whether page elements are available and clickable on the page

ElementToBeClickable (By locator)

The page element is selected

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 value

TextToBePresentInElementValue (By locator, java.lang.String text)

Title (title)

TitleContains (java.lang.String title)

Only if the explicit wait condition is met, the test code will continue to execute the subsequent test logic backward.

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 textbook\\ Selenium Webdriver\\ set_timeout.html"); WebDriverWait wait = new WebDriverWait (driver, 20); 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);} Thank you for reading this article carefully. I hope the article" sample Analysis of selenium Intelligent waiting pages in java "shared by the editor will be helpful to you. At the same time, I also hope you will support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report