6

I am trying to run my application in headless mode using Chrome Browser, Selenium and Java. But it open a new chrome instance and start running the script in normal UI mode(can see the execution)

Is there anything wrong in code or browser compatibility.

OS - Windows 10, Chrome Version - 85.0.4183.121

Below is my code -

package XYZ;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Test{
    public static void main(String args[]) {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("headless");
        options.addArguments("window-size=1400,800");       
        options.addArguments("disable-gpu")
        //options.addArguments("--headless", "--disable-gpu", "--window-size=1400,800","--ignore-certificate-errors");
        WebDriver driver = new ChromeDriver(options);   
        driver.get("https://www.google.com");
        System.out.println(driver.getCurrentUrl());
    }
}

2 Answers 2

8

I can see you are missing "--" before passing the arguments in the code. Below is the correct code to use headless chrome while running your automated cases:

    package XYZ;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Test{
    public static void main(String args[]) {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");     
        options.addArguments("--disable-gpu");
        options.addArguments("--window-size=1400,800");  
        WebDriver driver = new ChromeDriver(options);   
        driver.get("https://www.google.com");
        System.out.println(driver.getCurrentUrl());
    }
}

For me this is working fine. I hope this solves your probelm.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I have tried that as well but issue was with selenium webdriver version. After updating the selenium webdriver ot works for both cases.
So, right now after you have updated your chrome version to 85.0.4183.121, you have are facing this issue. Means headless option is not working?
Actually I was using chrome version as 85.0.4183.121 only earlier, but my selenium-java dependency was not updated. So i have updated the selenium-java dependency to 3.141.59 and I am able to run my code in headless mode.
2

This is the new way to do it:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
WebDriver driver = new ChromeDriver(options);

1 Comment

What do you mean by the new way to do it? Is the other answer no longer applicable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.