【问题标题】:While initiating the ChromeDriver getting an exception启动 ChromeDriver 时出现异常
【发布时间】:2014-03-07 21:52:09
【问题描述】:

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

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

还尝试从 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();
        }
    }

}

异常:下面是我从 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 版本:21.0.1171.0

操作系统:Windows 7 64 位。

Selenium Web 驱动版本:2.39.0

【问题讨论】:

  • 你用firefox试过代码吗?您可以删除 System.setProperty() 并直接调用 driver = new FirefoxDriver() 。我还看到您的代码中本地主机的拼写错误。

标签: java selenium selenium-webdriver selenium-chromedriver timeoutexception


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您对需要将 Selenium 指向什么感到有些困惑。

    这里:

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

    这个设置,webdriver.chrome.driver 是 Selenium 将读取的设置,以找出 ChromeDriver 的位置,不是 实际 Chrome安装。

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

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

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

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

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

    【讨论】:

    • 非常感谢您的回答。我已经下载了 ChromeDriver。现在我得到了例外。请帮忙..
    • 已启动 ChromeDriver 端口=2275 版本=14.0.836.0 .\test\automation\proxy_launcher.cc(89):错误:值:app_launched 实际:1 预期:AUTOMATION_SUCCESS 这是:0 等待时出错来自浏览器进程的自动化 ping .\test\automation\proxy_launcher.cc(223):错误:值:automation()->GetBrowserWindowCount(&window_count) 实际:false 预期:true .\test\automation\proxy_launcher.cc(244) :错误:值:browser_proxy.get() 实际:false 预期:true
    • .\test\automation\proxy_launcher.cc(269):错误:值:WaitForBrowserProcessToQuit(TestTimeouts::action_max_timeout_ms(), &exit_code) 实际:false 预期:true .\test\automation\proxy_launcher .cc(270):错误:值:exit_code 实际:-1 预期:0 org.openqa.selenium.WebDriverException:无法启动或连接到 Chrome。请检查 ChromeDriver 是否是最新的。在以下位置使用 Chrome 二进制文件:C:\Users\Bnaresh\AppData\Local\Google\Chrome\Application\chrome.exe(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:51.73 秒
    • 构建信息:版本:'2.39.0',修订:'ff23eac',时间:'2013-12-16 16:12:12' 系统信息:主机:'01hw535163',ip: '10.72.15.53', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_17' 驱动信息: org.openqa.selenium.chrome .ChromeDriver 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 在 java.lang.reflect .Constructor.newInstance
    • 几件事,a) 您使用的是哪个版本的 Chrome?从此处重新下载 ChromeDriver:chromedriver.storage.googleapis.com/index.html?path=2.9 ...并使用您获得的完整堆栈跟踪详细信息编辑您的原始帖子。