zoukankan      html  css  js  c++  java
  • 怎么等待页面元素加载完成

    怎么等待页面元素加载完成

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

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

     

    明确的等待

     

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

     

     

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

     

     

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

     

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

     

    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.   
    10. public class WaitForSomthing {  
    11.   
    12.     /** 
    13.      * @author gongjf 
    14.      */  
    15.     public static void main(String[] args) {  
    16.         // TODO Auto-generated method stub  
    17.         System.setProperty("webdriver.firefox.bin","D:\Program Files\Mozilla Firefox\firefox.exe");    
    18.         WebDriver dr = new FirefoxDriver();  
    19.         String url = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html";// "/Your/Path/to/Wait.html"  
    20.         dr.get(url);  
    21.         WebDriverWait wait = new WebDriverWait(dr,10);  
    22.         wait.until(new ExpectedCondition<WebElement>(){  
    23.             @Override  
    24.             public WebElement apply(WebDriver d) {  
    25.                 return d.findElement(By.id("b"));  
    26.             }}).click();  
    27.           
    28.         WebElement element = dr.findElement(By.cssSelector(".red_box"));  
    29.         ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = "5px solid yellow"",element);    
    30.           
    31.     }  
    32. }  

     

    上 面的代码WebDriverWait类的构造方法接受了一个WebDriver对象和一个等待最长时间(10秒)。然后调用until方法,其中重写了 ExpectedCondition接口中的apply方法,让其返回一个WebElement,即加载完成的元素,然后点击。默认情况下,WebDriverWait每500毫秒调用一次ExpectedCondition,直到有成功的返回,当然如果超过设定的值还没有成功的返回,将抛出异常。

     

    隐性等待

     

    隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间。默认值是0,但是设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用。上面的代码就变成了这样:

    1. import java.util.concurrent.TimeUnit;  
    2.   
    3. import org.openqa.selenium.By;  
    4. import org.openqa.selenium.JavascriptExecutor;  
    5. import org.openqa.selenium.WebDriver;  
    6. import org.openqa.selenium.WebElement;  
    7. import org.openqa.selenium.firefox.FirefoxDriver;  
    8. import org.openqa.selenium.support.ui.ExpectedCondition;  
    9. import org.openqa.selenium.support.ui.WebDriverWait;  
    10.   
    11.   
    12. public class WaitForSomthing {  
    13.   
    14.     /** 
    15.      * @author gongjf 
    16.      */  
    17.     public static void main(String[] args) {  
    18.         // TODO Auto-generated method stub  
    19.         System.setProperty("webdriver.firefox.bin","D:\Program Files\Mozilla Firefox\firefox.exe");    
    20.         WebDriver dr = new FirefoxDriver();  
    21.           
    22.         //设置10秒  
    23.         dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
    24.           
    25.         String url = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html";// "/Your/Path/to/Wait.html"  
    26.         dr.get(url);  
    27.                  //注释掉原来的  
    28.         /*WebDriverWait wait = new WebDriverWait(dr,10); 
    29.         wait.until(new ExpectedCondition<WebElement>(){ 
    30.             @Override 
    31.             public WebElement apply(WebDriver d) { 
    32.                 return d.findElement(By.id("b")); 
    33.             }}).click();*/  
    34.         dr.findElement(By.id("b")).click();  
    35.         WebElement element = dr.findElement(By.cssSelector(".red_box"));  
    36.         ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = "5px solid yellow"",element);    
    37.           
    38.     }  
    39. }  

     

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

  • 相关阅读:
    CF763C Timofey and Remoduling
    CF762E Radio Stations
    CF762D Maximum Path
    CF763B Timofey and Rectangles
    URAL1696 Salary for Robots
    uva10884 Persephone
    LA4273 Post Offices
    SCU3037 Painting the Balls
    poj3375 Network Connection
    Golang zip压缩文件读写操作
  • 原文地址:https://www.cnblogs.com/timssd/p/5101435.html
Copyright © 2011-2022 走看看