chromedriver 不再是 运行,因此 ChromeDriver 假设 Chrome 通过 Python 使用 Selenium 时出现崩溃错误

chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed error using Selenium through Python

开始之前:我知道有十亿篇关于 Selenium 无法正常工作的帖子,以及可以尝试的各种解决方案。我相信我已经尝试了一切,但如果我遗漏了什么,请原谅我。我正在用头撞墙,希望得到帮助。

以下是我采取的一些步骤:

我下载了 selenium 的 chromedriver(Ubuntu、Python)并使用 chmod 755chmod 777 使驱动程序可执行。之后,我用 ./chromedriver.

启动了 chromedriver

我已经为 Selenium 尝试了各种选项,包括手动添加 chromedriver 运行 on

的端口
from selenium import webdriver

options = webdriver.ChromeOptions()
options.binary_location = "/home/myname/projects/myproject/chromedriver"
options.add_argument("--remote-debugging-port=9515")
chrome_driver_binary = '/home/myname/projects/myproject/chromedriver'
driver = webdriver.Chrome(chrome_driver_binary, options = options)
driver.get('http://www.ubuntu.com/')

我尝试过其他帖子中建议的选项,例如:

options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--disable-setuid-sandbox")

我确定我使用的 chromedriver 与我的 Chrome 版本兼容。

似乎没有任何效果。我不断收到此错误:

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (chrome not reachable)
  (The process started from chrome location /home/myname/projects/myproject/chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

我衷心感谢其他人对这个问题的解释。

您需要处理以下几件事:

  • options.binary_location:指的是 binary location and is used if Google Chrome isn't installed at the default location. See:

  • --remote-debugging-port: 如果你不是远程调试,你可以安全地删除这个参数。

  • chrome_driver_binary:指在你系统中的绝对位置。

  • webdriver.Chrome(chrome_driver_binary, options = options):另外你可能想添加 key executable_path如下:

    chrome_driver_binary = '/home/myname/projects/myproject/chromedriver'
    driver = webdriver.Chrome(executable_path=chrome_driver_binary, options = options)
    driver.get('http://www.ubuntu.com/')
    
  • --no-sandbox,--headless,--disable-dev-shm-usage--disable-setuid-sandbox 等是您可能不需要开始的可选设置。

启动 driven initiated 浏览上下文的最小代码块可以是:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(executable_path='/home/myname/projects/myproject/chromedriver', options=options)
driver.get("http://www.ubuntu.com/")

A common cause for Chrome to crash during startup is running Chrome as root user (administrator) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.


参考资料

您可以在以下位置找到一些相关的详细讨论: