在Ubuntu 16.04 LTS上运行Headless Chrome时出现异常错误java.lang.NoClassDefFoundError:org / apache / commons / exec / ExecuteWatchdog

时间:2017-09-06 12:59:50

标签: ubuntu selenium-webdriver google-chrome-headless

我一直试图在Ubuntu 16.04 LTS上运行无头镀铬,已下载最新的Chrome驱动程序(2.32 for Linux x64) 这是代码,我写了以下代码:

package com.test.tests;

import static org.testng.Assert.assertTrue;

import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;

public class ChromeHeadlessTest {

    @Test
    public void testExecution() throws IOException {
        System.setProperty("webdriver.chrome.driver", "resources/linux/chromedriver");

        // Add options to Google Chrome. The window-size is important for responsive sites
        ChromeOptions options = new ChromeOptions();
        options.addArguments("headless");
        options.addArguments("window-size=1200x600");

        WebDriver driver = new ChromeDriver(options);
        driver.get("https://google.co.in");

        // a guarantee that the test was really executed
        assertTrue(driver.findElement(By.id("hplogo")).isDisplayed());

        driver.quit();
    }
}

当我将此测试作为TestNG Test运行时,我收到以下错误:

[RemoteTestNG] detected TestNG version 6.8.2
[TestNG] Running:
  /tmp/testng-eclipse-1039732738/testng-customsuite.xml

FAILED: testExecution
java.lang.NoClassDefFoundError: org/apache/commons/exec/ExecuteWatchdog
    at org.openqa.selenium.os.CommandLine.<init>(CommandLine.java:38)
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:176)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:184)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:171)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:158)
    at com.highq.test.tests.ChromeHeadlessTest.testExecution(ChromeHeadlessTest.java:24)
    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:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.exec.ExecuteWatchdog
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 35 more

我已经搜索了很多修复但不能。

提前致谢

3 个答案:

答案 0 :(得分:0)

第一次错误

首先,你的标题错误与apache poi jar错误有关,可以通过以下解决方案删除。

添加commons-logging.jar应解决此类问题

commons-logging.jar

https://commons.apache.org/proper/commons-logging/download_logging.cgi

第二次错误

根据您发布的错误,您的Chrome二进制文件的路径不正确,或者Chrome二进制文件未放置在同一位置。

您需要从以下位置下载linux二进制文件

https://chromedriver.storage.googleapis.com/index.html?path=2.32/

注意: - 根据您的操作系统下载二进制文件,在您的情况下似乎是

希望它会对你有所帮助:)。

答案 1 :(得分:0)

问题是您的驱动程序未初始化。 从这一行:

The driver executable does not exist: /usr/bin/chrominum-browser

首先确保chromedriver.exe存在于上述文件夹中。

如果是这种情况,那么您可以尝试不同的初始化,如下所示:

driver = webdriver.Chrome(chrome_options=options);

答案 2 :(得分:0)

这里你错过了commons-exec-1.3 jar文件。尝试添加jar文件,它将正常工作 https://commons.apache.org/proper/commons-exec/download_exec.cgi