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:
- https://forums.docker.com/t/cant-connect-to-other-containers-inside-docker-network/66512
- https://forums.docker.com/t/docker-compose-doesnt-let-my-images-connect-with-each-other/54951
Posts I've examined which did not work:
- Docker: using container with headless Selenium Chromedriver
- docker selenium/standalone-chrome unable to connect to docker web server
- Easiest way to run Selenium tests in a Docker container over Jenkins CI
- Selenium webdriver.Remote driver does not work with tor proxy(webdriver.Chrome does)
- How do I link and scale multiple docker containers?
- How to point RemoteWebDriver to one of the multiple standalone docker selenium standalone chrome browsers?
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.