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

来源:互联网 发布:阿里云邮的企业版 编辑:程序博客网 时间:2024/05/17 01:26

用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 windows7 C:\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就行。

0 2