【问题标题】:Selenium with webdriver.Firefox() crashes in GitHub Actions带有 webdriver.Firefox() 的 Selenium 在 GitHub Actions 中崩溃
【发布时间】:2021-02-19 10:22:00
【问题描述】:

我正在尝试使用 GitHub Actions 运行 selenium,但 selenium 崩溃了:

Traceback (most recent call last):
File "main.py", line 2, in <module>
webdriver.Firefox()
File "/opt/hostedtoolcache/Python/3.8.2/x64/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/opt/hostedtoolcache/Python/3.8.2/x64/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/opt/hostedtoolcache/Python/3.8.2/x64/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/opt/hostedtoolcache/Python/3.8.2/x64/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/opt/hostedtoolcache/Python/3.8.2/x64/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process

ma​​in.py

from selenium import webdriver
webdriver.Firefox()

.github/workflows/test.yml

name: Python application

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8.2
      uses: actions/setup-python@v2
      with:
        python-version: 3.8.2
    - name: geckodriver/firefox
      run: |
        echo "geckodriver/firefox"
        which geckodriver
        geckodriver --version
        which firefox
        firefox --version
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Test selenium
      run: |
        python main.py

requirements.txt

selenium==3.141.0

这是整个项目。我在其他项目中遇到了同样的问题,我试图在这里隔离问题。

geckodriver/firefox 步骤打印

geckodriver/firefox
/usr/bin/geckodriver
geckodriver 0.27.0 (7b8c4f32cdde 2020-07-28 18:16 +0000)

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
/usr/bin/firefox
Mozilla Firefox 82.0

我猜这里的 geckodriver.log 中可能有一些有趣的信息,但是我不知道在崩溃后如何访问 geckodriver.log。我想一个选项是将 geckodriver 记录到标准输出,但我还没有找到任何方法。

我已经尝试在 Python 3.6 和 Python 3.7 上运行它。同样在 Ubuntu 18.04 上。但我没有任何运气。

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 您可能需要在无头模式下启动 Firefox 以进行 Github Actions。
  • @pcalkins 就是这样!谢谢您的帮助。我会在这里发布答案。

标签: python selenium selenium-webdriver github-actions


【解决方案1】:

上面的 pcalkins 发布了解决方案。在 GitHub Actions 上,Firefox 必须以无头模式启动。这有效:

from selenium import webdriver

options = webdriver.FirefoxOptions()
options.headless = True
webdriver.Firefox(options=options)

【讨论】:

    猜你喜欢
    • 2019-02-11
    • 2021-05-20
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2020-07-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多