google-chromeselenium-webdriverselenium-chromedriver

Selenium 4.29.0 unable to create Chrome session


I've recently updated from Selenium 4.27.0 to 4.29.0 and I am getting the following error when trying to run a Selenium test:

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir 
Host info: host: '<< SERVER NAME >>', ip: '<< SERVER IP >>'
Build info: version: '4.29.0', revision: '5fc1ec94cb'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.1.0-32-amd64', java.version: '17.0.14'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--headless, --disable-dev-shm-usage], binary: /usr/bin/google-chrome, extensions: []}}]}]
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:114)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:75)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:61)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:187)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:245)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:174)
    at org.openqa.selenium.chromium.ChromiumDriver.(ChromiumDriver.java:110)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:83)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:72)

My code doesn't use --user-data-dir, but I have tried giving it a path and I get the same error.

Here is what I use to create the ChromeDriver, the error occurs on the last line:

ChromeOptions options = new ChromeOptions();

HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("download.default_directory", downloadFolder.getAbsolutePath());
options.setExperimentalOption("prefs", chromePrefs);

options.addArguments("--headless");
options.addArguments("--disable-dev-shm-usage");

WebDriver driver = new ChromeDriver(options);

I've also tested it without any arguments, and it gives the same error.

Does anyone have any suggestions for what the issue may be?


Solution

  • I found 2 solutions to my issue.

    Running these as root required the --no-sandbox Chrome option to work.

    But it also turns out the issue was with Tomcat needing access to its home folder.
    So a better solution for me was to add /var/lib/tomcat to ReadWritePaths in /etc/systemd/system/tomcat10.service.d/override.conf.

    Doubt it will help anyone else in future, but those were my solutions.