使用Selenium Web Driver加载Chrome浏览器时出现问题

9
我无法在Chrome浏览器中运行我的测试。我正在使用Selenium-Java 2.33.0和Java进行编码。
以下是步骤:
System.setProperty("webdriver.chrome.driver", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
WebDriver driver  = new ChromeDriver();
driver.get("http://www.google.com");

它打开了Chrome浏览器并抛出以下错误。

Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException:     Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.32.0', revision: '6c40c187d01409a5dc3b7f8251859150c8af0bcb', time: '2013-04-09 10:39:28'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.4', java.version: '1.6.0_35'
Driver info: driver.version: ChromeDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:111)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:115)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:161)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:107)
at com.integration.RandomID.main(RandomID.java:14)
Caused by: org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
Build info: version: '2.32.0', revision: '6c40c187d01409a5dc3b7f8251859150c8af0bcb', time: '2013-04-09 10:39:28'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.4', java.version: '1.6.0_35'
Driver info: driver.version: ChromeDriver
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:165)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:62)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
... 6 more
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:17036/status] to be available after 20018 ms
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:104)
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:163)
... 8 more
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143)
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:79)
... 9 more
Caused by: java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:228)
at java.util.concurrent.FutureTask.get(FutureTask.java:91)
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130)
... 10 more

我错在哪里了?

谢谢, Manoj


这表示驱动程序无法访问链接 http://localhost:17036/status,可能是由于端口问题,操作系统可能已经将该端口用于其他用途。请尝试重新启动您的计算机。 - Sajan Chandran
@SajanChandran 我也尝试重新启动了我的电脑,但是没有什么效果 :( - user2649233
你需要下载Chrome驱动程序 https://code.google.com/p/chromedriver/downloads/list 并将其指定为webdriver.chrome.driver变量。 - art1go
2个回答

23
不要使用你用于浏览互联网的Chrome二进制文件,从Chromedriver网站下载,并在你的代码中指定其路径。
这个链接有更详细的解决方案:http://www.gjdb.nl/?p=214
当你想在Mac上为WebDriver运行Chrome时,根据http://code.google.com/p/selenium/wiki/ChromeDriver网站,你需要将浏览器的路径指定为以下位置:“/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome”。
因此,创建ChromeDriver实例的Java代码如下:
System.setProperty("webdriver.chrome.driver", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
driver = new ChromeDriver();
尝试运行时出现以下异常。
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.8.3', java.version: '1.6.0_45'
Driver info: driver.version: ChromeDriver
...

我通过使用ChromeDriver来解决了这个问题。在http://code.google.com/p/chromedriver/downloads/list上下载你的版本,解压缩并按如下方式使用:

System.setProperty("webdriver.chrome.driver", "<unzip location>/chromedriver");
driver = new ChromeDriver();

2
请注意,在您的计算机上仍需要安装Chrome浏览器。驱动程序只是一个包装器。 - Sridhar Sarnobat

-1
在开始菜单的搜索栏中搜索chrome.exe,复制文件位置并将其粘贴到URL中:
System.setProperty("webdriver.chrome.driver", ".....AppData/Local/Google/Chrome/Application/chrome.exe");
WebDriver wcr = new ChromeDriver();

OP正在Mac OS X系统上,而不是Windows系统。 - Daishi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接