Selenium: WebDriverException: Chrome启动失败:因为Google Chrome不再运行,所以ChromeDriver认为Chrome已经崩溃。

115

最近我换了电脑,自那以后就无法使用selenium启动Chrome。我还尝试了Firefox,但浏览器实例就是无法启动。

from selenium import webdriver

d = webdriver.Chrome('/home/PycharmProjects/chromedriver')

d.get('https://www.google.nl/')

我遇到了以下错误:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=2.43.600233, platform=Linux 4.15.0-38-generic x86_64)

我安装了最新的Chrome版本和Chromedriver。
编辑: 尝试了@b0sss的解决方案后,我遇到了以下错误。
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (chrome not reachable)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so chromedriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.15.0-38-generic x86_64)
22个回答

2

2

不要以root用户身份运行脚本(在我的情况下)。


1
面对这个问题,我试图在WSL2中使用Pycharm调试器运行/调试Python Selenium脚本。第一个解决方案是使用--headless模式,但我更喜欢在调试过程中拥有Chrome GUI。
在Pycharm调试器之外的系统终端中,通过设置DISPLAY环境变量的方式(按照这里的指南here),Chrome GUI工作得很好: export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0 不幸的是,在调试期间,~/.bashrc不会在Pycharm中运行,因此导出操作无法正常工作。
我让Chrome GUI从Pycharm调试器中工作的方法是:在WSL2中运行echo $DISPLAY,将IP地址(你会得到类似于这样的东西)172.18.144.1:0粘贴到Pycharm Debug Configuration > Environment Variables中:

enter image description here


在克隆问题中找到了类似的解决方案,这也可能有所帮助 https://dev59.com/XFUL5IYBdhLWcg3wOl9x#57403093 - klapshin

1
这个错误在过去的六个月中一直随机出现在我的测试运行中(在Chrome 76和Chromedriver 76上仍然会发生),并且只在Linux上发生。平均每几百次测试中就会有一个失败,然后下一个测试就能正常运行。
无法解决这个问题,所以我在Python中将 driver = webdriver.Chrome() 包装在 setUp() 中的try..except块中,这是我所有测试的基类。如果它遇到了Webdriver异常,它会等待十秒钟然后再次尝试。
它解决了我遇到的问题,虽然不够优雅但是有效。
from selenium import webdriver
from selenium.common.exceptions import WebDriverException

try:
    self.driver = webdriver.Chrome(chrome_options=chrome_options, desired_capabilities=capabilities)
except WebDriverException as e:
    print("\nChrome crashed on launch:")
    print(e)
    print("Trying again in 10 seconds..")
    sleep(10)
    self.driver = webdriver.Chrome(chrome_options=chrome_options, desired_capabilities=capabilities)
    print("Success!\n")
except Exception as e:
    raise Exception(e)

我在使用selenium==4.0.0.b1时遇到了错误,但是selenium==4.0.0.a7可以正常工作,只有在Linux上出现问题,在Windows上没有问题。 - MortenB
capabilities 未定义。 - aL_eX

1

我在 Linux 环境下遇到了这个错误。如果不使用 headless,则需要

from sys import platform
    if platform != 'win32':
        from pyvirtualdisplay import Display
        display = Display(visible=0, size=(800, 600))
        display.start()

1

请确保 chromedrivergoogle-chrome 可执行文件都具有执行权限

sudo chmod -x "/usr/bin/chromedriver"

sudo chmod -x "/usr/bin/google-chrome"

sudo chmod +x "/usr/bin/chromedriver"sudo chmod +x "/usr/bin/google-chrome" - Atif Hussain

0
在我的情况下,Chrome出现了故障。以下两行代码解决了这个问题:
apt -y update; apt -y upgrade; apt -y dist-upgrade
apt --fix-broken install

0
我曾遇到同样的问题。我在终端上执行了"sudo geany"命令,但您应该不使用"sudo",只需在终端上输入"geany"即可解决。

0
也许在本地开发时,您使用了 options.headless=False 以便查看浏览器的操作,但是您忘记在虚拟机中将其更改为 True。

这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Simas Joneliunas

0

在运行脚本之前,通过终止远程服务器上运行的所有Chrome进程来解决了问题。这可能解释了为什么一些建议您以root身份运行脚本的答案有效。

$ pkill -9 chrome
$ ./my_script.py

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接