【问题标题】:Running Selenium on python:3.7 Container在 python:3.7 容器上运行 Selenium
【发布时间】:2021-10-22 12:17:57
【问题描述】:

我正在尝试使用基本 python:3.7 映像运行 Selenium 脚本,但我有点不确定如何安装所需的依赖项。我遇到了其他几个关于使用不同图像(如 selenium/standalone-chrome)的问题,但是,我还有其他几个不使用 Selenium 在此容器上运行的脚本,所以我有点犹豫是否要更改它。

我可以在这张图片上运行 Selenium 吗?或者,如果我将其更改为 selenium/standalone-chrome,它会影响我的应用程序的其余部分。我正在运行一个烧瓶 API。

这是我现在面临的错误,

selenium.common.exceptions.WebDriverException: Message: Service <path>  unexpectedly exited. Status code was: 127

请注意,我只安装了 pip 的 selenium,而没有安装其他依赖项,例如 chrome。

【问题讨论】:

    标签: python docker selenium


    【解决方案1】:

    正确的用例是让selenium/standalone-chrome 容器运行并单独让一个容器运行测试代码。

    如果您使用 docker-compose 运行“集群”,并且您的测试在 `RemoteWebDriver' 的帮助下引用远程 selenium 容器。

    【讨论】:

    • 我实际上并没有在这里运行测试。我使用 Selenium 编写了一个脚本来从网站上抓取一些数据。所以,我基本上有一些数据管道可以从各种来源中提取数据。我只需要 Selenium 用于这些管道之一。
    • 无论如何。任何与 webdriver 通信的客户端代码都必须在单独的容器中执行..
    【解决方案2】:

    我能够在这里找到我需要的东西, https://dev.to/nazliander/using-selenium-within-a-docker-container-ghp

    可以将以下命令添加到您的 Dockerfile 以安装 Chrome 驱动程序以运行 Selenium,

    # Adding trusting keys to apt for repositories
    RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
    # Adding Google Chrome to the repositories
    RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
    # Updating apt to see and install Google Chrome
    RUN apt-get -y update
    # Install Google Chrome
    RUN apt-get install -y google-chrome-stable
    # Installing Unzip
    RUN apt-get install -yqq unzip
    # Download the Chrome Driver
    RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
    # Unzip the Chrome Driver into /usr/local/bin directory
    RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
    # Set display port as an environment variable
    ENV DISPLAY=:99
    

    请注意,这适用于我一直在使用的 python:3.7 基础映像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2019-05-24
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多