Headless chrome using Selenium-Java running tests in normal UI mode of browser
Asked Answered
D

2

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());
    }
}
Devastation answered 24/9, 2020 at 12:27 Comment(0)
W
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.

Worldwide answered 24/9, 2020 at 12:59 Comment(3)
Yes, I have tried that as well but issue was with selenium webdriver version. After updating the selenium webdriver ot works for both cases.Devastation
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?Worldwide
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.Devastation
G
1

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);
Gower answered 12/3 at 16:18 Comment(1)
What do you mean by the new way to do it? Is the other answer no longer applicable?Rosebud

© 2022 - 2024 — McMap. All rights reserved.