[WebDriver]等待页面加载,(转载出自jarvi)

  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_x_x('div');  
  16.                 d.className "red_box" 
  17.                 document.body.a(d);  
  18.              
  19.         </script>  
  20.     </head>  
  21.     <body>  
  22.         <button id "b" onclick "show_div()">click</button>  
  23.     </body>  
  24. </html>  

 

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

 

Java代码  (){ @Override public WebElement apply(WebDriver d) { return d.findElement(By.id("b")); }}).click(); WebElement element = dr.findElement(By.cssSelector(".red_box")); ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = \"5px solid yellow\"",element); } }" quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">  收藏代码
  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.     public static void main(String[] args)  
  14.         // TODO Auto-generated method stub  
  15.         System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");    
  16.         WebDriver dr new FirefoxDriver();  
  17.         String url "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html";// "/Your/Path/to/Wait.html"  
  18.         dr.get(url);  
  19.         WebDriverWait wait new WebDriverWait(dr,10);  
  20.         wait.until(new ExpectedCondition<WebElement>(){  
  21.             @Override  
  22.             public WebElement apply(WebDriver d)  
  23.                 return d.findElement(By.id("b"));  
  24.             }}).click();  
  25.           
  26.         WebElement element dr.findElement(By.cssSelector(".red_box"));  
  27.         ((JavascriptExecutor)dr).executeScript("arguments[0].style.border \"5px solid yellow\"",element);    
  28.           
  29.      
  30.  

 

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

 

隐性等待

 

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

 

Java代码    收藏代码
  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.     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.           
  20.         //设置10秒  
  21.         dr.manage().timeouts().implicitlyWait(10TimeUnit.SECONDS);  
  22.           
  23.         String url "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html";// "/Your/Path/to/Wait.html"  
  24.         dr.get(url);  
  25.                  //注释掉原来的  
  26.           
  27.         dr.findElement(By.id("b")).click();  
  28.         WebElement element dr.findElement(By.cssSelector(".red_box"));  
  29.         ((JavascriptExecutor)dr).executeScript("arguments[0].style.border \"5px solid yellow\"",element);    
  30.           
  31.      
  32.  

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值