Java selenium smart waiting for page loading to complete sample code, selenium sample code

Source: Internet
Author: User

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

  1. Instance requirements
  2. Implicit wait
  3. 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!

Related Article

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.