selenium(webdriver)学习笔记1--ChromeDriver

用webdriver启动firefox时很简单,以下代码就可以。

WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.close();

但是启动Google Chrome浏览器,就比较复杂了。

如果还是用以下代码,会报错。

WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.close();

错误信息:

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://chromedriver.storage.googleapis.com/index.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:110)
    at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:118)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:291)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:82)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:117)
    at webdrivers.UseChromeDriver.main(UseChromeDriver.java:9)

解决办法:下载chromedriver

下载地址: https://sites.google.com/a/chromium.org/chromedriver/downloads
请查看自己Google Chrome的版本下载对应的chromedriver.
对了,还有一点,下图是某一个版本的所有chromedriver,没有win64的,我试验过了
win64的操作系统用win32的就行。
chromedriver
下载好之后解压,得到chromedriver,代码变更如下:

System.setProperty("webdriver.chrome.driver", "D:/drivers/chromedriver_win32-2.14/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.close();

原因:

ChromeDriver 是一个standalone server,是跟the Chromium team 合作开发的。
它由三部分组成,chrome浏览器,Selenium project (“the driver”) 提供的language bindings(这个我也不知道是啥,我猜是selenium-chrome-driver-*.jar),还有就是可执行的chromedriver,就是之前程序里缺少的。这个可执行的chromedriver是另外两个的桥梁。

server要求chrome浏览器安装在默认路径

OS安装路径
Linux/usr/bin/google-chrome
Mac/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
windows7C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
Windows XP%HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe

对于linux系统,/usr/bin/google-chrome是真正安装路径的symlink就行。

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值