11

I've been having trouble getting Python selenium to connect to selenium/standalone-chrome, and was looking for insight on how to fix my issue. I would like to avoid using selenium/hub, but including it does not seem to fix my issue.

Here is my docker-compose.yml

version: '3.1'

networks:
  web:
    external: true

services:

  chrome:
    image: selenium/standalone-chrome:latest
    hostname: chrome
    networks:
      - web
    ports:
      - "5900:5900"
      - "4444:4444"
    privileged: true
    shm_size: 2g

  tests:
    build: ./tests
    networks:
      - web

And the test I'm running inside the test container. The entrypoint checks to make sure that chrome is up and running before running the scripts.

#!/usr/bin/env python3
"""Tests that the remote webdriver works."""
import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class LocalGoogleTestCase(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Chrome()
        self.addCleanup(self.browser.quit)

    def testPageTitle(self):
        self.browser.get('http://www.google.com')
        self.assertIn('Google', self.browser.title)


class RemoteGoogleTestCase(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Remote(
            command_executor='http://chrome:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.CHROME)
        self.addCleanup(self.browser.quit)

    def testPageTitle(self):
        self.browser.get('http://www.google.com')
        self.assertIn('Google', self.browser.title)


if __name__ == '__main__':
    unittest.main(verbosity=2)

For the test results, the Local test succeeds, it's only when trying to use the remote. Occasionally I will get the error hub not found in PATH, but that error is intermittent.

I am able to access the web interface via http://server:444/wd/hub and can start sessions and run scripts from there.

I believe this may be an issue related to containers not being able to reach out to each other and I have evaluated the following resources for trying to workout that issue:

Posts I've examined which did not work:

Thanks for looking!

Update: From within the tests container, I am able to curl http://chrome:4444/wd/hub/status to retrieve the status that the connection is up and running, and this is part of the entryscript.sh, so I know the containers can talk to each other in some fashion.

sempervent
  • 833
  • 2
  • 11
  • 23

2 Answers2

14

First, of al, I'd like to thank you for all this. After reaching this post it gave me the hope that I'm not the one who is trying to do such a thing.

So, the thing is I'm successfully able to run everything from docker-compose and everything executed as expected.

Got hints from your post and made a few changes and it actually worked.

Here goes the solution.

FileName: docker-compose.yml

version: '3.8'

networks:
    web:
      external: true
      driver:  bridge

services:
    chrome:
        image: selenium/standalone-chrome:latest
        hostname: chrome
        networks:
          - web
        privileged: true
        shm_size: 2g
    framework:
        build: .
        networks:
            - web
        depends_on: 
            - chrome

Also, note that the grid url is http://chrome:4444/wd/hub With this change in configuration, I was able to run my code successfully and I was also able to send out emails.

I hope this helps someone who is stuck with docker-compose.yml

Abhishek Puri
  • 345
  • 3
  • 6
1

Thanks for the solution I was struggling with the same error, but in my case the fail was that I didn't add the following lines:

privileged: true
shm_size: 2g

I will add just one "fix" if you want to call it to your solution. As all the containers are built under the same docker-compose file you don't need to create a network, it is automatically created for all of them.

version: '3.8'

services:
    chrome:
        image: selenium/standalone-chrome:latest
        hostname: chrome
        privileged: true
        shm_size: 2g
    framework:
        build: .
        depends_on: 
            - chrome

Once again. Thanks!