skip to Main Content

I’m trying to use the Chrome inside my Docker for my Selenium project.
Here is how I start Docker

docker run -d -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:latest

Then, in my code, I’m trying to use the RemoteWebDriver to connect to my Docker

val chromeOptions = ChromeOptions()
chromeOptions.addArguments("--no-sandbox")
chromeOptions.addArguments("--headless");
val url = URL("http://localhost:4444/wd/hub")
webDriver = RemoteWebDriver(url, chromeOptions)
val baseUrl = "https://www.google.com"
webDriver.get(baseUrl)
webDriver.quit()

But then, I’m receiving response code 500 error from Selenium.

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) 
Host info: host: 'c3d9edcf7df3', ip: '172.17.0.2'
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.20.1'
Driver info: driver.version: unknown
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.20.1'
Driver info: driver.version: unknown
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '13.5.1', java.version: '11.0.16.1'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--no-sandbox, --headless], extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--no-sandbox, --headless], extensions: []}}}]
Capabilities {browserName: chrome, goog:chromeOptions: {args: [--no-sandbox, --headless], extensions: []}}
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:148)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:106)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
        at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:51)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:139)

How can I use Docker for Selenium?

My Selenium version:

  • selenium-java:4.8.1
  • selenium-chrome-driver:4.8.1

I also tried to upgrade to the latest Selenium version which is:

  • selenium-java:4.12.1
  • selenium-chrome-driver:4.12.1

EDIT:
After adding the --disable-dev-shm-usage and --remote-debugging-port=9222
here is my new Error message

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: session not created: Chrome failed to start: exited normally.
  (chrome not reachable)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) 
Host info: host: '419674405ad2', ip: '172.17.0.2'
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.20.1'
Driver info: driver.version: unknown
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '11.0.20.1'
Driver info: driver.version: unknown
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '13.5.1', java.version: '11.0.16.1'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --no-sandbox, --disable-dev-shm-usage, --headless, --disable-extensions, --remote-debugging-port=9222], extensions: []}}]}]
Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --no-sandbox, --disable-dev-shm-usage, --headless, --disable-extensions, --remote-debugging-port=9222], extensions: []}}
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:96)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:68)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
        at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:51)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:163)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:143)

2

Answers


  1. Try to add --disable-dev-shm-usage argument just after --no-sandbox:

    chromeOptions.addArguments("--disable-dev-shm-usage");
    

    If it doesn’t work, try to add also this:

    chromeOptions.addArguments("--remote-debugging-port=9222")
    
    Login or Signup to reply.
  2. You may need to add some more arguments to ChromeOptions to ensure stable execution inside the Docker container.

    val chromeOptions = ChromeOptions()
    chromeOptions.addArguments("--no-sandbox")
    chromeOptions.addArguments("--headless")
    chromeOptions.addArguments("--disable-gpu")
    chromeOptions.addArguments("--disable-dev-shm-usage")
    chromeOptions.addArguments("--remote-debugging-port=9222")
    
    1. --disable-gpu: Disables the GPU hardware acceleration. That is generally useful in headless environments.
    2. --disable-dev-shm-usage: That flag will force Chrome to use disk instead of /dev/shm for shared memory, which may resolve crashing issues.
    3. --remote-debugging-port=9222: see Chrome DevTools protocol

    Note: a SessionNotCreatedException is usually indicative of a mismatch between Chrome and ChromeDriver versions, or it can also be due to limited resources available on the system. Since you are using a Docker image for Selenium Standalone Chrome, the version mismatch is unlikely. You can, however, verify this by checking the logs when the Docker container starts up.

    Taking into account the additional ChromeOptions and Docker configurations, the updated code should look like:

    val chromeOptions = ChromeOptions()
    chromeOptions.addArguments("--no-sandbox")
    chromeOptions.addArguments("--headless")
    chromeOptions.addArguments("--disable-gpu")
    chromeOptions.addArguments("--disable-dev-shm-usage")
    chromeOptions.addArguments("--remote-debugging-port=9222")
    val url = URL("http://localhost:4444/wd/hub")
    webDriver = RemoteWebDriver(url, chromeOptions)
    val baseUrl = "https://www.google.com"
    webDriver.get(baseUrl)
    webDriver.quit()
    

    Try running your Selenium test again with these changes. If the issue persists, check the Docker logs for the Selenium container to gain more insight into what might be causing the problem (docker logs <container_id>).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search