Java 启动 ChromeDriver 时出现异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21679357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:02:40  来源:igfitidea点击:

While initiating the ChromeDriver getting an exception

javaseleniumselenium-webdriverselenium-chromedrivertimeoutexception

提问by Naresh

I'm very new to Selenium. Below given is my first test script.

我对硒很陌生。下面给出的是我的第一个测试脚本。

Question: Does it require selenium server to test a local website (which is hosted in the same machine as the test scripts being run).

问题:是否需要 selenium 服务器来测试本地网站(与正在运行的测试脚本托管在同一台机器上)。

Also tried executing the same script from Internet Explorer also, still getting the same. It just opens the browser and closes(because of finally block) it.

还尝试从 Internet Explorer 执行相同的脚本,仍然得到相同的结果。它只是打开浏览器并关闭(因为 finally 阻止)它。

import org.openqa.selenium.chrome.ChromeDriver;
public class TestScript {
    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) {
        ChromeDriver driver = null;
        try 
        { 
            System.setProperty("webdriver.chrome.driver", "C:\Users\user1\AppData\Local\Google\Chrome\Application\chrome.exe");
            driver = new ChromeDriver();
            System.out.println("Opening the Browser");
            driver.get("http://localhsot:5080/myWebSite/8450191#");
            System.out.println("Open the Browser");
            System.out.println("");
            System.out.println("Title" +driver.getTitle());

        }
        catch (Exception ie)
        {
            ie.printStackTrace();
        }
        finally
        {
            System.out.println("Quitting the Browser");
            driver.close();
            driver.quit();
        }
    }

}

Exception: Below is the exception I am getting while executing from the Eclipse:

异常:以下是我从 Eclipse 执行时遇到的异常:

[2820:6204:36503609:ERROR:gpu_info_collector_win.cc(93)] Can't retrieve a valid WinSAT assessment.
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.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: '01hw535163', ip: '10.72.15.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17'
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:112)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:116)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:162)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:108)
    at com.seic.scripts.TestScript.main(TestScript.java:16)
Caused by: org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: '01hw535163', ip: '10.72.15.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17'
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:37571/status] to be available after 20009 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
Quitting the Browser
Exception in thread "Main Thread" java.lang.NullPointerException
    at com.scripts.TestScript.main(TestScript.java:31)

Chrome Version: 21.0.1171.0

铬版本:21.0.1171.0

OS: Windows 7 64 bit.

操作系统:Windows 7 64 位。

Selenium Web Driver version:2.39.0

Selenium Web 驱动程序版本:2.39.0

回答by Arran

You are getting a little confused about what you need to point Selenium to.

您对需要将 Selenium 指向的内容感到有些困惑。

Here:

这里:

System.setProperty("webdriver.chrome.driver", "C:\Users\user1\AppData\Local\Google\Chrome\Application\chrome.exe");

This setting, webdriver.chrome.driveris a setting that Selenium will read to find out where the ChromeDriveris located, notthe actualChrome installation.

这样设置后,webdriver.chrome.driver是一个设置硒会读,找出其中ChromeDriver所在,没有实际的Chrome安装。

https://code.google.com/p/selenium/wiki/ChromeDriver

https://code.google.com/p/selenium/wiki/ChromeDriver

You must download the ChromeDriver and where it is located should be what you pass in for the webdriver.chrome.driverproperty.

您必须下载 ChromeDriver,它所在的位置应该是您为该webdriver.chrome.driver属性传入的内容。

I imagine it's the same reason when you try with IE, it also requires a driver which I guess you don't have:

我想这与您尝试使用 IE 时的原因相同,它还需要一个我猜您没有的驱动程序:

https://code.google.com/p/selenium/wiki/InternetExplorerDriver

https://code.google.com/p/selenium/wiki/InternetExplorerDriver

In terms of whether you need a "server" -> for your tests, no, you are not running them remotely. Once you decide to run them remotely (i.e use the RemoteWebDriverinstead of ChromeDriver) then you will need the Selenium server.

就您是否需要“服务器”-> 进行测试而言,不,您没有远程运行它们。一旦您决定远程运行它们(即使用RemoteWebDriver代替ChromeDriver),那么您将需要 Selenium 服务器。

回答by NaviSaysListen

I would recommend instead of using System.setProperty() to put your drivers in the same folder and point to that folder in System Variables Path. Then you don't need to point to it at all in code and updating your driver won't require a code rewrite.

我建议不要使用 System.setProperty() 将驱动程序放在同一个文件夹中,并在系统变量路径中指向该文件夹。然后你根本不需要在代码中指向它,更新你的驱动程序不需要重写代码。