如何解决Java项目中的Selenium Chromeriver超时问题?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (391)

我无法弄清楚如何正确配置selenium测试以在Ubuntu VM中运行。在Windows上,我有eclipse IDE,但工作正常。这是testNGclass的java代码的一部分:

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {   

    if (SystemUtils.IS_OS_LINUX)      
        System.setProperty("webdriver.chrome.driver", "/usr/bin/selenium/chromedriver/chromedriver");

    if(SystemUtils.IS_OS_WINDOWS)
        System.setProperty("webdriver.chrome.driver", ""+System.getProperty("user.dir").toString()+"/lib/chromedriver.exe");

    driver = new ChromeDriver();          

    System.out.println(System.getProperty("portletToTest"));  
    baseUrl = "http://www.google.it";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void test() throws Exception {
    driver.get(baseUrl + "/");

我一直得到的错误就是这个,来自testNG报告(除了localhost的端口:,每次运行时都会有所不同)

org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'esvn', ip: '192.168.5.182', os.name: 'Linux', os.arch: 'amd64', os.version: '3.5.0-54-generic', java.version: '1.8.0_121'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:178)
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:166)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:241)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:141)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:174)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:163)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:120)
    at Automation.profileuserportlet.setUp(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:169)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:756)
    at org.testng.TestRunner.run(TestRunner.java:610)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    at org.testng.TestNG.privateMain(TestNG.java:1434)
    at org.testng.TestNG.main(TestNG.java:1403)
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:17352/status] to be available after 20008 ms
    at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:107)
    at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:175)
    ... 34 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:80)
    ... 35 more
Caused by: java.util.concurrent.TimeoutException
    at java.util.concurrent.FutureTask.get(FutureTask.java:205)
    at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130)
    ... 36 more

我已经在linux上检查了chromedriver文件路径,没关系,文件具有可执行权限。我是否还必须在linux机器上启动chromedriver可执行文件?除了运行已编译的java项目,我还需要做些什么吗?

提问于
用户回答回答于

Chrome驱动程序应该有与Chrome浏览器匹配的正确版本

扫码关注云+社区

领取腾讯云代金券