首页 > 解决方案 > WebDriverException:消息:未知错误:Chrome 无法启动:在 Amazon Linux 上通过 Selenium Python 使用 ChromeDriver Chrome 时发生崩溃错误

问题描述

只是为了表明我已经尽职尽责,对于以下问题,我已经尝试过建议的答案,或者至少阅读并尝试理解:

Selenium:WebDriverException:Chrome 无法启动:由于 google-chrome 不再运行而崩溃,因此 ChromeDriver 假设 Chrome 已崩溃

从 chrome 位置 C:\..\Chrome\Application\chrome.exe 开始的进程不再运行,因此 ChromeDriver 假设 Chrome 已崩溃

Selenium python库通过docker,Chrome错误无法启动:异常退出

使用 Selenium 时 Chrome 崩溃(没有发布答案,但我仍然查看了它)

如何修复 Linux 中的“usr/bin/google-chrome 不再运行,因此 ChromeDriver 假设 Chrome 已崩溃”错误? - 对于这个,我用“/etc/alternatives/google-chrome”替换了“/usr/bin/google-chrome”,仍然没有用。

从 chrome 位置 /usr/bin/google-chrome 开始的进程不再运行,因此 ChromeDriver 假设 Chrome 已为 Selenium 崩溃

selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:在 Python 中使用 ChromeDriver 和 Selenium 崩溃

python linux selenium:chrome无法访问

未知错误:Chrome 无法启动:崩溃(硒,无头模式)

python selenium:WebDriverException:消息:chrome无法访问

Selenium chrome 无法启动

WebDriverException:消息:未知错误:Chrome 无法启动:通过 VPS 上的 Python 使用 ChromeDriver Chrome 和 Selenium 异常退出

使用 Chrome 浏览器在 Selenium Grid 中执行测试脚本时出现“Chrome 无法访问”错误

Selenium webdriver错误Chrome无法启动

org.openqa.selenium.WebDriverException:未知错误:Chrome 无法启动:崩溃(无头 chrome)

Python:Selenium - 消息:未知错误:Chrome 无法启动:异常退出

我在 Stack Overflow 上看到了一个常见错误,在我的 Amazon Linux 服务器上使用 Python 运行 Selenium 时,我得到以下结果:

Traceback (most recent call last):
  File "test-selenium-chrome.py", line 15, in <module>
    driver = webdriver.Chrome(options=options, executable_path='/usr/local/bin/chromedriver')  # Optional argument, if not specified will search path.i
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (chrome not reachable)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

这是我的代码:

#!/usr/bin/python3
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from pyvirtualdisplay import Display

options = Options()
options.binary_location = '/usr/bin/google-chrome'
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--remote-debugging-port=9515')
options.add_argument('--disable-setuid-sandbox')

display = Display(visible=0, size=(800, 800))
display.start()

driver = webdriver.Chrome(options=options, executable_path='/usr/local/bin/chromedriver')  # Optional argument, if not specified will seearch path.i
driver.maximize_window()
driver.get('http://www.google.com/')
time.sleep(5)  # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)  # Let the user actually see something!
driver.quit()

我正在使用 Google Chrome 版本 79.0.3945.130,以及 https://sites.google.com/a/chromium.org/chromedriver/downloads 中指定的相应 chromedriver 版本 ChromeDriver 79.0.3945.36

附加信息,如果我只是从命令行运行 google-chrome,我会得到:

[ec2-user@ip-xxx-xx-xx-xxx bin]$ pwd
/usr/bin
[ec2-user@ip-xxx-xx-x-xxx bin]$ google-chrome
Segmentation fault

任何帮助是极大的赞赏。

标签: pythonlinuxseleniumgoogle-chromeselenium-chromedriver

解决方案


此错误消息...

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (chrome not reachable)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

...意味着ChromeDriver无法启动/产生新的浏览上下文,即Chrome 浏览器会话。


根据Selenium 中的讨论: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver 假设 Chrome has crashed , 上的预期默认位置是:

/usr/bin/google-chrome

注意:对于 Linux 系统,ChromeDriver 期望/usr/bin/google-chrome成为实际 Chrome 二进制文件的符号链接。

因此,理想情况下,以下最小代码块应该可以工作:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = '/usr/bin/google-chrome'
driver = webdriver.Chrome(options=options, executable_path='/usr/local/bin/chromedriver')
driver.get('http://www.google.com/')

但似乎,当您尝试手动启动Chrome会话时,会发生分段错误,即崩溃如下:

[ec2-user@ip-xxx-xx-xx-xxx bin]$ pwd
/usr/bin
[ec2-user@ip-xxx-xx-x-xxx bin]$ google-chrome
Segmentation fault

分段故障

分段错误(缩写为segfault)或访问冲突是由具有内存保护的硬件引发的故障或失败情况,通知操作系统该软件已尝试访问内存的受限区域。作为响应,操作系统内核通常会执行一些纠正措施,通常通过向进程发送信号将故障传递给有问题的进程(您的脚本)。

简而言之,它是一种辅助机制,可以限制程序/脚本破坏不属于它的内存。在这里查看更多。


原因及解决方案

可能的原因和解决方案是:

  • 系统中根本没有安装Chrome,所以你必须安装Chrome
  • Chrome未安装在默认位置,因此您必须通过属性传递chrome可执行文件的正确位置。binary_location
  • 实际 Chrome 二进制文件的符号链接/usr/bin/google-chrome已损坏,因此您可能必须创建符号链接。
  • 用户没有所需的访问权限/usr/bin/google-chrome,因此您必须提供访问权限。

推荐阅读