python - 如何在 Docker 中使用 Chrome 运行 Selenium

标签 python python-2.7 selenium docker google-chrome

我在 Docker 中安装了 google-chrome ,但是当我运行我的 Python 2 Selenium的脚本,它像这样失败了:

automation@1c17781fef0c:/topology-editor/test$ python test.py
Traceback (most recent call last):
  File "test.py", line 27, in <module>
    browser = webdriver.Chrome()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 185, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.4.0-83-generic x86_64)

如果我直接在 docker 中运行 google-chrome,它会显示如下:

automation@1c17781fef0c:/topology-editor/test$ google-chrome
Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted
Trace/breakpoint trap (core dumped)
automation@1c17781fef0c:/topology-editor/test$

系统:

$ uname -a
Linux 1c17781fef0c 4.4.0-83-generic #106-Ubuntu SMP Mon Jun 26 17:54:43 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
$ google-chrome --version
Google Chrome 60.0.3112.78
$ chromedriver --version
ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8)

最佳答案

您需要启动一个独立的 chrome 浏览器

docker run -d -p 4444:4444 selenium/standalone-chrome

然后在你的 python 脚本中使用 Remote webdriver 启动浏览器

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", DesiredCapabilities.CHROME)

如果需要,您还可以启动 Selenium Grid 中心。

要将此作为 django 测试执行以下操作:

# docker-compose.yml

selenium:
  image: selenium/standalone-firefox
  ports:
  - 4444:4444

# project/app/test.py

from django.test import TestCase
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class SiteTest(TestCase):
    fixtures = [
        'app/fixtures/app.json',
        ...
    ]

    def setUp(self):
        self.browser = webdriver.Remote("http://selenium:4444/wd/hub", DesiredCapabilities.FIREFOX)

    def tearDown(self):
        self.browser.quit()

    def test_visit_site(self):
        self.browser.get('http://app:8000/')
        self.assertIn(self.browser.title, 'Home')

注意:

如果您使用 webdriver.ChromeOptions|FirefoxOptions|etc,则无需导入 DesiredCapabalities:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')  # example
driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", options=options)

关于python - 如何在 Docker 中使用 Chrome 运行 Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45323271/

相关文章:

python - 默认字典/按分钟排序

python - Python 的 word 文档的页数

python - 在 Numpy(或 Scipy)中计算斜率

python - 如何找到二维数组中每一行的最大值?

python - Windows 和 Linux 中正则表达式的不同结果?

python - 我在使用时遇到问题 (EC.presence_of_element_ located(By.class, ""))

python - 通过 shell 脚本一起运行 5 个 python 程序

python - 字符串变量作为pyplot中的 latex

javascript - 验收测试侧重于传出的 http 请求

python - 强制 Selenium 暂停并等待字段可用