0

I can run any of my tests in Chrome recapitated (headfull) mode but when I try to run them in headless mode, I get the following error.

[main] INFO io.github.bonigarcia.wdm.WebDriverManager - Using chromedriver 114.0.5735.90 (resolved driver for Chrome 114)
[main] INFO io.github.bonigarcia.wdm.WebDriverManager - Exporting webdriver.chrome.driver as C:\Users\user\.cache\selenium\chromedriver\win32\114.0.5735.90\chromedriver.exe
Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 49153
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1687204620.180][WARNING]: Browser-wide DevTools client failed to connect: disconnected: unable to connect to renderer

Step failed
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: disconnected: not connected to DevTools
  (failed to check if window was closed: disconnected: not connected to DevTools)
  (Session info: headless chrome=114.0.5735.134)
Build info: version: '4.4.0', revision: 'e5c75ed026a'
System info: host: , ip: , os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '19.0.1'

I have added the following chrome options, tried commenting a few out and running again based on this but I still get the same response.

        boolean isHeadless = true;
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("prefs", chromePrefs);
        chromeOptions.addArguments("use-fake-ui-for-media-stream");
        chromeOptions.addArguments("--window-size=1920,1080");
        chromeOptions.addArguments("--disable-gpu");
        chromeOptions.addArguments("--remote-allow-origins=*");
        chromeOptions.addArguments("lang=en-GB");
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--no-sandbox");
        chromeOptions.addArguments("--disable-dev-shm-usage");
        chromeOptions.setHeadless(true);

I expected it to run in headless mode as it does in recapitated mode.

2 Answers2

0

Instead of boolean isHeadless = true; and/or chromeOptions.setHeadless(true); you can use the argument --headless=new as follows:

ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--headless=new");
WebDriver driver = new ChromeDriver(options);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

if this helps upvote and check my answer!

first thing i notice is that you are using the installed chrome drive, i recommend using the dependencie :

<dependency>
  <groupId>io.github.bonigarcia</groupId>
  <artifactId>webdrivermanager</artifactId>
  <version>5.3.3</version>
</dependency>

it will allow you to use chrome driver and firefox and others as i think

you might wanted to use :

        option.setPageLoadStrategy(PageLoadStrategy.NONE);

get inspired from this example:

    @BeforeTest
public void setUp(){
    WebDriverManager.chromedriver().setup();
    option = new ChromeOptions();
    option.addArguments("--remote-allow-origins=*");
    option.addArguments("--disable-blink-features=AutomationControlled");
    option.setPageLoadStrategy(PageLoadStrategy.EAGER);

    driver = new ChromeDriver(option);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    WebDriverManager.chromedriver().setup();
    option = new ChromeOptions();
    option.addArguments("--remote-allow-origins=*");
    option.addArguments("--disable-blink-features=AutomationControlled");
    option.addArguments("--disable-gpu");
    option.addArguments("--disable-dev-shm-usage");
    option.addArguments("--no-sandbox");
    option.addArguments("--headless"); // Run in headless mode
}
roudlek
  • 160
  • 9
  • Unfortunately, it did not work, thanks for ur answer. :) – Aydin Apaydin Jun 19 '23 at 22:00
  • @AydinApaydin please provide more information, tell me exactly your purpose of this operation – roudlek Jun 20 '23 at 09:39
  • I was trying to set up my Driver class that would work for Jenkins as well. However, I found the problem, someone added chromeOptions.addArguments("--remote-debugging-port=9222"); in browser switch case part which caused error in output. – Aydin Apaydin Jun 20 '23 at 22:49