Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using selenium/standalone-chrome in docker-compose connecting with Python's selenium

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.

like image 386
sempervent Avatar asked May 14 '20 18:05

sempervent


2 Answers

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

like image 58
Abhishek Puri Avatar answered Nov 15 '22 09:11

Abhishek Puri


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!

like image 45
MiguelAndreu Avatar answered Nov 15 '22 09:11

MiguelAndreu