如何使用selenium判断页面加载 webdriver来判断一个网页加载完毕

selenium调用IEDriver打开IE_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
selenium调用IEDriver打开IE
上传于||文档简介
&&s​e​l​e​n​i​u​m​ ​ ​I​E​D​r​i​v​e​r​ ​ ​I​E
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢selenium WebDriver 浏览器操作_Office教程学习网
selenium WebDriver 浏览器操作
1.启动浏览器
//打开默认路径的firefox(路径指的是 firefox 的安装路径)
WebDriver diver = new FirefoxDriver();
//打开指定路径的firefox,方法1
System.setProperty(&webdriver.firefox.bin&,&D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe&);
WebDriver dr = new FirefoxDriver();
//打开指定路径的firefox,方法2
File pathToFirefoxBinary = new File(&D:\\Program Files\\Mozilla Firefox\\firefox.exe&);
FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
WebDriver driver1 = new FirefoxDriver(firefoxbin,null);
WebDriver ie_driver = new InternetExplorerDriver();
因为Chrome Driver是Chromium 项目自己支持和维护的,所以你必需另外下载chromedriver.exe,放在目录下C:\WINDOWS\system32
下载地址: /p/chromedriver/downloads/list
//打开chrome
WebDriver driver = new ChromeDriver();
另一种启动chrome 的方法
wiki介绍:/p/selenium/wiki/ChromeDriver
//打开chrome
System.setProperty(&webdriver.chrome.driver&, &D:\\chromedriver.exe&);
System.setProperty(&webdriver.chrome.bin&,
C:\\Documents and Settings\\fy\\Local Settings&
+&\\Application Data\\Google\\Chrome\\Application\\chrome.exe&);
Chromium介绍:/p/chromium/
2.页面跳转url
String url = &&;
WebDriver driver = new FirefoxDriver();
A//用get方法
driver.get(url);
B//用navigate方法,然后再调用to方法,chrome不支持这种方法
driver.navigate().to(url);
3.关闭浏览器
//quit 关闭所有页面 close 关闭本次执行打开的页面
A.//用quit方法
driver.quit();
B.//用close方法
driver.close();
4.获取页面信息
//得到title
String title = driver.getTitle();
//得到当前页面url
String currentUrl = driver.getCurrentUrl();
getWindowHandle() 返回当前的浏览器的窗口句柄
getWindowHandles() 返回当前的浏览器的所有窗口句柄
getPageSource() 返回当前页面的源码
//String s=driver.getPageSource();s=s.substring(s.indexOf(&{&), s.indexOf(&}&));
//System.out.println(&当前页面的源码:&+s);
操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。
源代码这些方法都是在 org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承 RemoteWebDriver。
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------如何使用selenium webdriver来判断一个网页加载完毕_百度知道26243人阅读
Selenium+自动化测试(115)
selenium webdriver学习(十一)------------如何等待页面元素加载完成
web的自动化测试中,我们经常会遇到这样一种情况:当我们的程序执行时需要页面某个元素,而此时这个元素还未加载完成,这时我们的程序就会报错。怎么办?等待。等待元素出现后再进行对这个元素的操作。
在selenium-webdriver中我们用两种方式进行等待:明确的等待和隐性的等待。
明确的等待
明确的等待是指在代码进行下一步操作之前等待某一个条件的发生。最不好的情况是使用Thread.sleep()去设置一段确认的时间去等待。但为什么说最不好呢?因为一个元素的加载时间有长有短,你在设置sleep的时间之前要自己把握长短,太短容易超时,太长浪费时间。selenium webdriver提供了一些方法帮助我们等待正好需要等待的时间。利用WebDriverWait类和ExpectedCondition接口就能实现这一点。
下面的html代码实现了这样的一种效果:点击click按钮5秒钟后,页面上会出现一个红色的div块。我们需要写一段自动化脚本去捕获这个出现的div,然后高亮之。
&title&Set Timeout&/title&
.red_box {background-color: width = 20%; height: 100 border:}
function show_div(){
setTimeout(&create_div()&, 5000);
function create_div(){
d = document.createElement('div');
d.className = &red_box&;
document.body.appendChild(d);
&button id = &b& onclick = &show_div()&&click&/button&
下面的代码实现了高亮动态生成的div块的功能:
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptE
import org.openqa.selenium.WebD
import org.openqa.selenium.WebE
import org.openqa.selenium.firefox.FirefoxD
import org.openqa.selenium.support.ui.ExpectedC
import org.openqa.selenium.support.ui.WebDriverW
public class WaitForSomthing {
* @author gongjf
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty(&webdriver.firefox.bin&,&D:\\Program Files\\Mozilla Firefox\\firefox.exe&);
WebDriver dr = new FirefoxDriver();
String url = &file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html&;// &/Your/Path/to/Wait.html&
dr.get(url);
WebDriverWait wait = new WebDriverWait(dr,10);
wait.until(new ExpectedCondition&WebElement&(){
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);
上面的代码WebDriverWait类的构造方法接受了一个WebDriver对象和一个等待最长时间(10秒)。然后调用until方法,其中重写了ExpectedCondition接口中的apply方法,让其返回一个WebElement,即加载完成的元素,然后点击。默认情况下,WebDriverWait每500毫秒调用一次ExpectedCondition,直到有成功的返回,当然如果超过设定的值还没有成功的返回,将抛出异常。
隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间。默认值是0,但是设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用。上面的代码就变成了这样:
import java.util.concurrent.TimeU
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptE
import org.openqa.selenium.WebD
import org.openqa.selenium.WebE
import org.openqa.selenium.firefox.FirefoxD
import org.openqa.selenium.support.ui.ExpectedC
import org.openqa.selenium.support.ui.WebDriverW
public class WaitForSomthing {
* @author gongjf
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty(&webdriver.firefox.bin&,&D:\\Program Files\\Mozilla Firefox\\firefox.exe&);
WebDriver dr = new FirefoxDriver();
//设置10秒
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String url = &file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html&;// &/Your/Path/to/Wait.html&
dr.get(url);
//注释掉原来的
/*WebDriverWait wait = new WebDriverWait(dr,10);
wait.until(new ExpectedCondition&WebElement&(){
public WebElement apply(WebDriver d) {
return d.findElement(By.id(&b&));
}}).click();*/
dr.findElement(By.id(&b&)).click();
WebElement element = dr.findElement(By.cssSelector(&.red_box&));
((JavascriptExecutor)dr).executeScript(&arguments[0].style.border = \&5px solid yellow\&&,element);
两者选其一,第二种看起来一劳永逸呀。哈哈
博主 用隐性等待是查找之前会等么 不管元素出现与否?
对的,如果一个无素没有出现都会默认等待你所设定的时间,直到超时或者元素出现
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4097020次
积分:42056
积分:42056
排名:第60名
原创:273篇
转载:1330篇
译文:18篇
评论:128条
文章:58篇
阅读:176271
文章:21篇
阅读:79984
(5)(12)(9)(27)(16)(11)(8)(3)(3)(9)(9)(2)(3)(2)(24)(26)(10)(25)(20)(28)(12)(27)(38)(17)(5)(17)(22)(74)(10)(8)(44)(15)(3)(1)(1)(8)(5)(2)(17)(8)(23)(13)(55)(92)(64)(45)(56)(82)(49)(111)(98)(49)(117)(125)(12)(3)(1)(6)(1)(1)(15)(3)(8)(4)如何使用selenium webdriver来判断一个网页加载完毕_百度知道

我要回帖

更多关于 如何判断网页加载完毕 的文章

 

随机推荐