java - 启动 ChromeDriver 时出现异常

标签 java selenium selenium-webdriver selenium-chromedriver timeoutexception

我对 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

最佳答案

您对需要将 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 服务器。

关于java - 启动 ChromeDriver 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21679357/

相关文章:

python - 警告(来自警告模块): ResourceWarning: unclosed <socket. socket object, fd=404, family=2, type=1, proto=0> using selenium

java - 如何使用 WebDriver for Java 选择动态号码链接

java - 我如何从下拉菜单中选择元素

python - 在python中进行全页截图而不分页

java - 如何通过 webdriver 测试输入字段功能的屏蔽

java - 最小化 Java 中的垃圾收集尝试(示例)

java - spring中将@Controller替换为@RestController的效果

javascript - 如何在独立模式下调试 webdriverio?

java - 硬币找零的空间优化解决方案

java - 接口(interface)参数为空