本文介绍了在docker-compose中使用selenium / standalone-chrome与Python的selenium连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很难让Python硒连接到 selenium / standalone-chrome ,并且一直在寻找有关如何解决问题的见解。我想避免使用 selenium / hub ,但是包含它似乎并不能解决我的问题。

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.

此处是我的docker-compose.yml

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)

对于测试结果, Local 测试成功,仅在尝试使用遥控器时。有时,我会收到错误 hub在PATH中找不到,但该错误是间歇性的。

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.

我能够通过 http:// server:444 / wd / hub 访问Web界面,并可以从此处开始会话和运行脚本。

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?

感谢您的关注!

更新:从测试容器中,我能够 curl http:// chrome:4444 / wd / hub / status 检索连接已建立并正在运行的状态,这是 entryscript.sh的一部分,所以我知道这些容器可以以某种方式相互通信。

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.

推荐答案

首先,好的,我要谢谢你。到达这篇文章后,它给了我希望,我不是尝试这样做的人。

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.

所以,事情是我能够成功地运行docker-compose,一切按预期执行。

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.

这里介绍了解决方案

文件名:docker-compose.yml

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

也,请注意,网格URL为 http:// chrome:4444 / wd / hub
通过此配置更改,我能够成功运行代码,并且我也能够发送电子邮件。

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

我希望这可以帮助那些被docker-compose.yml困扰的人

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

这篇关于在docker-compose中使用selenium / standalone-chrome与Python的selenium连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 14:26