selenium webdriver 打开Firefox和chrome
2016-06-12 15:12
351 查看
http://my.oschina.net/dyhunter/blog/94090
一、Firefox
普通情况下,直接运行代码就可以看到会自动弹出 FireFox 窗口,访问 baidu.com,然后输入关键字并查询,一切都是自动完成的。
错误提醒:
1)Exception
in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
出现这个错误,是说明你的 FireFox 文件并没有安装在默认目录下,这时候需要在最开始执行:System.setProperty
设置环境变量 "webdriver.firefox.bin" 将自己机器上 FireFox 的正确路径设置完毕后即可。
2)Exception
in thread "main" org.openqa.selenium.UnsupportedCommandException: Bad request
出现这个错误,很有意思。 查了一下 有人说应该是 hosts 出现了问题,加上一个 127.0.0.1 localhost 就行了,但我的 hosts 上肯定有这个玩意,为啥也会出现这个问题呢?
经过调试,发现 127.0.0.1 localhost 的设置必须要在 hosts 文件的最开始,而且如果后面有其他设置后,也不要再出现同样的 127.0.0.1 localhost ,只要有就会出错。(因为我为了方便访问 google 的网站,专门加入了 smarthosts 的内容,导致了 localhost 的重复)
【3.
测试 Chrome】
Chrome 虽然不是 Selenium 的原配,但是没办法,她太火辣了,绝对不能抛下她不管的。
把 ExampleForFireFox.java 稍微修改就可以制作出一个 ExampleForChrome.java
,直接把 new FireFoxDriver() 修改为 new ChromeDriver() 你会发现还是行不通。
错误如下:
1)Exception
in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded
from http://code.google.com/p/chromedriver/downloads/list 这应该是找不到 chrome 的文件,好吧,利用 System.setProperty 方法添加路径,这里要注意,是
“webdriver.chrome.driver” 可不是“webdriver.chrome.bin”
设置路径后还是会报错:
2)[6416:4580:1204/173852:ERROR:gpu_info_collector_win.cc(91)]
Can't retrieve a valid WinSAT assessment.
这个貌似是因为 Selenium 无法直接启动 Chrome 导致的,必须要通过前面咱们下载 Chrome
的第三方插件 ChromeDriver,
【4. 测试 IE】
想逃避 IE 吗?? 作为前端开发,IE 你是必须要面对的,冲吧!
其实你会发现, Selenium 主要也就是针对 FireFox 和 IE 来制作的,所以把 FireFox
的代码修改为 IE 的,那是相当的容易,只需要简单地两步:
1)把 ExampleForFireFox.java 另存为 ExampleForIE.java
2)把 WebDriver driver = new FirefoxDriver(); 修改为 WebDriver
driver = new InternetExplorerDriver();
3)一般大家的 IE都是默认路径吧,所以也就不用设置 property 了
一、Firefox
package lesson1; import org.openqa.selenium.By; 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 ExampleForFireFox { public static void main(String[] args) { // 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置 // System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe"); // 创建一个 FireFox 的浏览器实例 WebDriver driver = new FirefoxDriver(); // 让浏览器访问 Baidu driver.get("http://www.baidu.com"); // 用下面代码也可以实现 // driver.navigate().to("http://www.baidu.com"); // 获取 网页的 title System.out.println("1 Page title is: " + driver.getTitle()); // 通过 id 找到 input 的 DOM WebElement element = driver.findElement(By.id("kw")); // 输入关键字 element.sendKeys("zTree"); // 提交 input 所在的 form element.submit(); // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().endsWith("ztree"); } }); // 显示搜索结果页面的 title System.out.println("2 Page title is: " + driver.getTitle()); //关闭浏览器 driver.quit(); } }
普通情况下,直接运行代码就可以看到会自动弹出 FireFox 窗口,访问 baidu.com,然后输入关键字并查询,一切都是自动完成的。
错误提醒:
1)Exception
in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
出现这个错误,是说明你的 FireFox 文件并没有安装在默认目录下,这时候需要在最开始执行:System.setProperty
设置环境变量 "webdriver.firefox.bin" 将自己机器上 FireFox 的正确路径设置完毕后即可。
2)Exception
in thread "main" org.openqa.selenium.UnsupportedCommandException: Bad request
出现这个错误,很有意思。 查了一下 有人说应该是 hosts 出现了问题,加上一个 127.0.0.1 localhost 就行了,但我的 hosts 上肯定有这个玩意,为啥也会出现这个问题呢?
经过调试,发现 127.0.0.1 localhost 的设置必须要在 hosts 文件的最开始,而且如果后面有其他设置后,也不要再出现同样的 127.0.0.1 localhost ,只要有就会出错。(因为我为了方便访问 google 的网站,专门加入了 smarthosts 的内容,导致了 localhost 的重复)
【3.
测试 Chrome】
Chrome 虽然不是 Selenium 的原配,但是没办法,她太火辣了,绝对不能抛下她不管的。
把 ExampleForFireFox.java 稍微修改就可以制作出一个 ExampleForChrome.java
,直接把 new FireFoxDriver() 修改为 new ChromeDriver() 你会发现还是行不通。
错误如下:
1)Exception
in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded
from http://code.google.com/p/chromedriver/downloads/list 这应该是找不到 chrome 的文件,好吧,利用 System.setProperty 方法添加路径,这里要注意,是
“webdriver.chrome.driver” 可不是“webdriver.chrome.bin”
设置路径后还是会报错:
2)[6416:4580:1204/173852:ERROR:gpu_info_collector_win.cc(91)]
Can't retrieve a valid WinSAT assessment.
这个貌似是因为 Selenium 无法直接启动 Chrome 导致的,必须要通过前面咱们下载 Chrome
的第三方插件 ChromeDriver,
只需要把 ‘webdriver.chrome.driver’ 的值设置为 chromedriver 的路径就可以了。
package lesson1; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForChrome2 { public static void main(String[] args) throws IOException { // 设置 chrome 的路径 System.setProperty( "webdriver.chrome.driver", "E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe"); // 创建一个 ChromeDriver 的接口,用于连接 Chrome // 创建一个 Chrome 的浏览器实例 WebDriver driver = new ChromeDriver(); // 让浏览器访问 Baidu driver.get("http://www.baidu.com"); // 用下面代码也可以实现 // driver.navigate().to("http://www.baidu.com"); // 获取 网页的 title System.out.println("1 Page title is: " + driver.getTitle()); // 通过 id 找到 input 的 DOM WebElement element = driver.findElement(By.id("kw")); // 输入关键字 element.sendKeys("zTree"); // 提交 input 所在的 form element.submit(); // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().endsWith("ztree"); } }); // 显示搜索结果页面的 title System.out.println("2 Page title is: " + driver.getTitle()); // 关闭浏览器 driver.quit(); // element = driver.findElement(By.id("kw")); // // element.clear(); // element.click(); // element.clear(); // element.sendKeys("zTree"); // element.submit(); } }
【4. 测试 IE】
想逃避 IE 吗?? 作为前端开发,IE 你是必须要面对的,冲吧!
其实你会发现, Selenium 主要也就是针对 FireFox 和 IE 来制作的,所以把 FireFox
的代码修改为 IE 的,那是相当的容易,只需要简单地两步:
1)把 ExampleForFireFox.java 另存为 ExampleForIE.java
2)把 WebDriver driver = new FirefoxDriver(); 修改为 WebDriver
driver = new InternetExplorerDriver();
3)一般大家的 IE都是默认路径吧,所以也就不用设置 property 了
package lesson1; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class ExampleForIE { public static void main(String[] args) { // 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置 // System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe"); // 创建一个 FireFox 的浏览器实例 WebDriver driver = new InternetExplorerDriver(); // 让浏览器访问 Baidu driver.get("http://www.baidu.com"); // 用下面代码也可以实现 // driver.navigate().to("http://www.baidu.com"); // 获取 网页的 title System.out.println("1 Page title is: " + driver.getTitle()); // 通过 id 找到 input 的 DOM WebElement element = driver.findElement(By.id("kw")); // 输入关键字 element.sendKeys("zTree"); // 提交 input 所在的 form element.submit(); // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().endsWith("ztree"); } }); // 显示搜索结果页面的 title System.out.println("2 Page title is: " + driver.getTitle()); // 关闭浏览器 driver.quit(); } }
相关文章推荐
- 夏天吃什么菜好菜谱
- Dashgo D1蓝牙控制移动
- Json.net之JsonConvert
- Qt依赖VTK编程小问题
- 当NAT遇到PPTP
- MVP宋沄剑的博客
- 禁止select默认选择
- Java中13个原子操作类
- C#开发ActiveX网页截图控件
- 排名前10的H5、Js 3D游戏引擎和框架
- Javascript面向对象编程三
- memcimp
- sql view学习
- 作业四,1001
- PAT乙级—1004. 成绩排名 (20)-native
- POJ 1200 Crazy Search hash入门
- linux如何分大于2T的磁盘分区
- 数据库原理(六)--关系数据理论
- leetcode 47. Permutations II-排列|回溯算法
- ORACLE 专用服务器模式(DEDICATED)与共享服务器模式(SHARE)的区别