`

selenium webdriver学习(十一)------------如何等待页面元素加载完成

阅读更多

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

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


明确的等待


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



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

Wait.html:



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

 
 

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

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestWait {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		String url = "file:/C:/Users/jgong/Desktop/wait.html";
		driver.get(url);

		WebDriverWait wait = new WebDriverWait(driver, 10);

		driver.findElement(By.id("b")).click();

		WebElement wl = wait.until(new ExpectedCondition<WebElement>() {

			@Override
			public WebElement apply(WebDriver d) {
				return d.findElement(By.cssSelector(".red_box"));
			}
		});

		((JavascriptExecutor) driver).executeScript(
				"arguments[0].style.border='5px solid yellow'", wl);

	}

}

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

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestWait {

	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		// 设置10秒
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

		String url = "file:/C:/Users/jgong/Desktop/wait.html";
		driver.get(url);

		driver.findElement(By.id("b")).click();

		/*
		 * WebDriverWait wait = new WebDriverWait(driver, 10); WebElement wl =
		 * wait.until(new ExpectedCondition<WebElement>() {
		 * 
		 * @Override public WebElement apply(WebDriver d) { return
		 * d.findElement(By.cssSelector(".red_box")); } });
		 */

		WebElement wl = driver.findElement(By.cssSelector(".red_box"));
		((JavascriptExecutor) driver).executeScript(
				"arguments[0].style.border='5px solid yellow'", wl);

	}

}

 

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

 

分享到:
评论
10 楼 qi_ling2005 2013-11-26  
wb136959813 写道
JF,

是的
9 楼 wb136959813 2013-08-30  
JF,
8 楼 stephenwang1011 2013-01-29  
没有转载 按钮呢
7 楼 qi_ling2005 2012-08-28  
duangang312 写道
什么意思,是指所有的findElement操作都会默认等待10秒?


就是查找的最大时间是10秒。找到马上返回。找不到最多找10秒,报错
6 楼 duangang312 2012-08-21  
什么意思,是指所有的findElement操作都会默认等待10秒?
5 楼 qi_ling2005 2012-07-16  
shine22fmf 写道
运行第一个脚本的时候,可以正常运行并显示,但是下面也有错误
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".red_box"}
Command duration or timeout: 16 milliseconds
这是啥意思呢?


没有找到页面的元素,超时了
4 楼 shine22fmf 2012-07-13  
老师,第二个脚本可以正常显示,下面一个红条子,边缘是黄色的,第一个确实没有,请教下。
3 楼 shine22fmf 2012-07-13  
运行第一个脚本的时候,可以正常运行并显示,但是下面也有错误
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".red_box"}
Command duration or timeout: 16 milliseconds
这是啥意思呢?
2 楼 qi_ling2005 2012-05-11  
uniquepig 写道
博主 用隐性等待是查找之前会等么 不管元素出现与否?

对的,如果一个无素没有出现都会默认等待你所设定的时间,直到超时或者元素出现
1 楼 uniquepig 2012-05-11  
博主 用隐性等待是查找之前会等么 不管元素出现与否?

相关推荐

Global site tag (gtag.js) - Google Analytics