Hot questions for Using Ubuntu in google chrome

Top Java Programmings / Ubuntu / google chrome

Question:

I'm trying to start chrome headless through Java through processBuilder and here's my code :

pb = new ProcessBuilder(CHROME_UBUNTU_PATH, "--headless","--enable-logging", "--disable-gpu","--print-to-pdf=" + filePath + ".pdf","file:///" + absolutePath);
pb.start();

But chrome wouldn't start cause it's running with the user root :

Running as root without --no-sandbox is not supported.

So I want to know what user is a process is launched with and what should I do in this situation ?


Answer:

Simple: the process that runs this codes and tries to launch chrome is run by the root user. And because of that, the new process goes under the same user root.

Which is like the very last thing you want to do: you want to run as few jobs as possible with as root user.

In other words: the solution is to look into your setup and to ensure that you absolutely do not start any process as root yourself. (maybe there are exceptions where you have to, but again: that should be the absolute exception and only happen when there is no alternative for doing so).

Question:

I am using the Selenium API, but I got the error

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;

Expected location of Chrome is

/usr/bin/google-chrome

but in my computer it is in /usr/bin/google-chrome

I'm using Ubuntu, how I can fix this issue,


Answer:

After downloading the chrome driver from here - http://chromedriver.storage.googleapis.com/index.html?path=2.20/

you will have to add following lines to your code

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver();

Question:

I want to run headless Chrome with Selenium, but this code not works:

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--headless");
    File file = new File("/usr/bin/chromedriver");
    System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
    WebDriver driver = new ChromeDriver(chromeOptions);

It compiles fine, but at runtime crashes with stacktrace:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
    at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:93)
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:72)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.<init>(DriverCommandExecutor.java:62)
    at org.openqa.selenium.chrome.ChromeDriverCommandExecutor.<init>(ChromeDriverCommandExecutor.java:39)
    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 org.chrome.automation.Starter.main(Starter.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.http.auth.Credentials
    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)
    ... 8 more

The POM contains just two dependencies:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>

I thought the problem in Selenium dependencies, as they are rely on some apache code but not download dependent components. I tried to add apache dependencies like:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.6</version>
    </dependency>

But problem persists.

Please help to run selenium with headless Crome.


Answer:

I was able to resolve by using just one dependency:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>2.53.1</version>
    </dependency>

Also it works with 3.3.1, 3.2.0. As I see the problem happen in 3.4.0 version only.