java selenium Smart Wait Page Load Complete Sample Code

  • 2021-08-12 02:36:32
  • OfStack

java selenium Intelligent Wait Page Load Complete

We often encounter selenium to manipulate an element on a page, and 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 loading to be completed before we can operate

selenium provides a very simple and intelligent way to determine whether an element exists or not.

Reading catalog

Example requirements Implicit wait Explicit wait

Example requirements

Example: set_timeout. html below html code, click click button for 5 seconds, a red div will appear on the page. We need to write an automatic script to intelligently judge whether this div exists, and then highlight this div.


<html>
 <head>
  <title>Set Timeout</title>
  <style>
   .red_box {background-color: red; width = 20%; height: 100px; border: none;}
  </style>
  <script>
   function show_div(){
    setTimeout("create_div()", 5000);
   }
 
   function create_div(){
    d = document.createElement('div');
    d.className = "red_box";
    document.body.appendChild(d);
   }
  </script>
 </head>
 <body>
  <button id = "b" onclick = "show_div()">click</button>
 </body>
</html>

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); 

Among them

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

That means, wait a total of 10 seconds, and if the element does not exist after 10 seconds, the exception org. openqa. selenium. NoSuchElementException will be thrown

Explicit wait

Explicit waiting uses the method that comes with ExpectedConditions class, which can judge the explicit waiting.

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

等待的条件

WebDriver方法

页面元素是否在页面上可用和可被单击

elementToBeClickable(By locator)

页面元素处于被选中状态

elementToBeSelected(WebElement element)

页面元素在页面中存在

presenceOfElementLocated(By locator)

在页面元素中是否包含特定的文本

textToBePresentInElement(By locator)

页面元素值

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

标题 (title)

titleContains(java.lang.String title)

Only when the condition of explicit wait is met will the test code continue to execute the subsequent test logic backward

If the set maximum explicit wait time threshold is exceeded, the test program will throw 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); 
 }

The above is the Java selenium waiting for the page to load the data collation, follow-up to continue to supplement relevant information, thank you for your support to this site!


Related articles: