如何等待页面元素加载完成

来源:互联网 发布:怎样代理网络电话卡 编辑:程序博客网 时间:2024/05/29 18:36

web的自动化测试中,我们经常会遇到这样一种情况:当我们的程序执行时需要页面某个元素,而此时这个元素还未加载完成,这时我们的程序就会报错。怎么办?等待。等待元素出现后再进行对这个元素的操作。

在selenium-webdriver中我们用两种方式进行等待:明确的等待和隐性的等待。


明确的等待


明确的等待是指在代码进行下一步操作之前等待某一个条件的发生。最不好的情况是使用Thread.sleep()去设置一段确认的时间去等待。但为什么说最不好呢?因为一个元素的加载时间有长有短,你在设置sleep的时间之前要自己把握长短,太短容易超时,太长浪费时间。selenium webdriver提供了一些方法帮助我们等待正好需要等待的时间。利用WebDriverWait类和ExpectedCondition接口就能实现这一点。



下面的html代码实现了这样的一种效果:点击click按钮5秒钟后,页面上会出现一个红色的div块。我们需要写一段自动化脚本去捕获这个出现的div,然后高亮之。

Html代码  收藏代码
  1. Wait.html:  
  2.   
  3.   
  4.   
  5. <html>    
  6.    <head>    
  7.        <title>Set Timeout</title>    
  8.        <style>    
  9.            .red_box {background-color: red; width = 20%; height: 100px; border: none;}    
  10.        </style>    
  11.        <script>    
  12.             function show_div(){    
  13.                 setTimeout("create_div()", 5000);    
  14.             }    
  15.       
  16.             function create_div(){    
  17.                 d = document.createElement('div');    
  18.                 d.className = "red_box";    
  19.                 document.body.appendChild(d);    
  20.             }    
  21.         </script>    
  22.     </head>    
  23.     <body>    
  24.         <button id = "b" onclick = "show_div()">click</button>    
  25.     </body>    
  26. </html>    

 
 

下面的代码实现了高亮动态生成的div块的功能:

Java代码  收藏代码
  1. import org.openqa.selenium.By;  
  2. import org.openqa.selenium.JavascriptExecutor;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6. import org.openqa.selenium.support.ui.ExpectedCondition;  
  7. import org.openqa.selenium.support.ui.WebDriverWait;  
  8.   
  9. public class TestWait {  
  10.   
  11.     public static void main(String[] args) {  
  12.         WebDriver driver = new FirefoxDriver();  
  13.         String url = "file:/C:/Users/jgong/Desktop/wait.html";  
  14.         driver.get(url);  
  15.   
  16.         WebDriverWait wait = new WebDriverWait(driver, 10);  
  17.   
  18.         driver.findElement(By.id("b")).click();  
  19.   
  20.         WebElement wl = wait.until(new ExpectedCondition<WebElement>() {  
  21.   
  22.             @Override  
  23.             public WebElement apply(WebDriver d) {  
  24.                 return d.findElement(By.cssSelector(".red_box"));  
  25.             }  
  26.         });  
  27.   
  28.         ((JavascriptExecutor) driver).executeScript(  
  29.                 "arguments[0].style.border='5px solid yellow'", wl);  
  30.   
  31.     }  
  32.   
  33. }  

 
隐性的等待
隐性的等待其实就相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。

Java代码  收藏代码
  1. import java.util.concurrent.TimeUnit;  
  2. import org.openqa.selenium.By;  
  3. import org.openqa.selenium.JavascriptExecutor;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.FirefoxDriver;  
  7. import org.openqa.selenium.support.ui.ExpectedCondition;  
  8. import org.openqa.selenium.support.ui.WebDriverWait;  
  9.   
  10. public class TestWait {  
  11.   
  12.     public static void main(String[] args) {  
  13.         WebDriver driver = new FirefoxDriver();  
  14.         // 设置10秒  
  15.         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
  16.   
  17.         String url = "file:/C:/Users/jgong/Desktop/wait.html";  
  18.         driver.get(url);  
  19.   
  20.         driver.findElement(By.id("b")).click();  
  21.   
  22.         /* 
  23.          * WebDriverWait wait = new WebDriverWait(driver, 10); WebElement wl = 
  24.          * wait.until(new ExpectedCondition<WebElement>() { 
  25.          *  
  26.          * @Override public WebElement apply(WebDriver d) { return 
  27.          * d.findElement(By.cssSelector(".red_box")); } }); 
  28.          */  
  29.   
  30.         WebElement wl = driver.findElement(By.cssSelector(".red_box"));  
  31.         ((JavascriptExecutor) driver).executeScript(  
  32.                 "arguments[0].style.border='5px solid yellow'", wl);  
  33.   
  34.     }  
  35.   
  36. }  

 

两者选其一,第二种看起来一劳永逸呀。哈哈

0 0