Chetan Modi
Chetan Modi

Reputation: 63

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

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());
    }
}

Upvotes: 6

Views: 14313

Answers (2)

WhatdoesITmatter
WhatdoesITmatter

Reputation: 21

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);

Upvotes: 2

Prateek Srivastava
Prateek Srivastava

Reputation: 223

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.

Upvotes: 8

Related Questions