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个回答

175

请尝试下载此处链接的最新Chrome Driver版本:

尝试这个:

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

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
d = webdriver.Chrome('/home/<user>/chromedriver',chrome_options=chrome_options)
d.get('https://www.google.nl/')

我已经从那个位置下载了最新的chromedriver。 - SOeh
27
请确保 --no-sandbox 选项是第一个(不像这个例子,第一个选项是 --headless)。我不断地收到错误提示,直到我把 --no-sandbox 移到了选项列表的顶部,并将其添加为第一个选项。 - parsecer
5
请注意:from selenium.webdriver.chrome.options import Options 的翻译如下:从 selenium.webdriver.chrome.options 导入 Options - Jacob Stern
2
据我所知,--no-sandbox Chrome 选项参数就足够了。 - Maksim Kostromin
1
你需要匹配使用的chromedriver版本与Chromium版本。 - tarvinder91
显示剩余5条评论

41

This error message...

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.)

这意味着ChromeDriver无法启动/生成新的WebBrowser,即Chrome Browser会话。

您的主要问题是默认位置中未安装Chrome浏览器。

服务器即ChromeDriver期望您按照以下图像在每个系统的默认位置中安装Chrome

Chrome_binary_expected_location

1对于Linux系统,ChromeDriver期望/usr/bin/google-chrome是指向实际Chrome二进制文件的符号链接。


解决方案

如果您使用的是非标准位置的Chrome可执行文件,则必须按照以下方式覆盖Chrome二进制文件位置

  • Python Solution:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "C:\\path\\to\\chrome.exe"    #chrome binary location specified here
    options.add_argument("--start-maximized") #open Browser in maximized mode
    options.add_argument("--no-sandbox") #bypass OS security model
    options.add_argument("--disable-dev-shm-usage") #overcome limited resource problems
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get('http://google.com/')
    
  • Java Solution:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    ChromeOptions opt = new ChromeOptions();
    opt.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");  //chrome binary location specified here
    options.addArguments("start-maximized");
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    WebDriver driver = new ChromeDriver(opt);
    driver.get("https://www.google.com/");
    

2
设置 chrome 二进制文件已经生效。我给了你最佳答案。同时彻底删除 Chrome/Chromedriver/Pycharm,然后重新安装一切也行得通,这样现在我就不需要设置 chrome 二进制文件了。 - SOeh
selenium.common.exceptions.WebDriverException: Message: 未知错误:Chrome启动失败:已被终止。 (未知错误:DevToolsActivePort文件不存在)现在我得到了“已被终止”,而不是“已崩溃”。我已经在网上搜索了3个多小时,但似乎没有任何作用。 - MasterMind

19

希望这能帮助到某个人。这在我使用Ubuntu 18.10上有效。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=chrome_options)
driver.get('http://www.google.com')
print('test')
driver.close()

可在树莓派3B+(轻量级操作系统)上使用Chromium浏览器。 - Dhananjay Yelwande
可以使用已下载的驱动程序和驱动程序管理器正常工作。但是,使用服务对象会失败。 - Phume

9

我曾经遇到过类似的问题,并发现选项参数必须按特定顺序。我只知道在我的Ubuntu 18机器上需要两个参数才能让它正常工作。下面是我的示例代码:

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

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

d = webdriver.Chrome(executable_path=r'/home/PycharmProjects/chromedriver', chrome_options=options)
d.get('https://www.google.nl/')

2
使用 --no-sandbox 选项不是一个安全问题吗? - Mahak Malik

8

我在运行docker容器(在构建环境中)时遇到了同样的问题。在ssh进入容器后,我尝试手动运行测试,但仍然遇到了问题。

(unknown error: DevToolsActivePort file doesn't exist)
     (The process started from chrome location /usr/bin/google-chrome-stable is 
      no longer running, so ChromeDriver is assuming that Chrome has crashed.)

当我尝试在本地运行 Chrome /usr/bin/google-chrome-stable 时,出现了错误信息。

Running as root without --no-sandbox is not supported

我检查了我的ChromeOptions,发现缺少了--no-sandbox,这就是为什么无法启动chrome的原因。

capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
  chromeOptions: { args: %w(headless --no-sandbox disable-gpu window-size=1920,1080) }
)

1
另一种解决方法是不要将进程(在容器内)作为root运行。从安全角度来看,这样做更好。 :) - XtraSimplicity

7

对于RobotFramework

我解决了这个问题!使用 --no-sandbox

${chrome_options}=  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
Call Method    ${chrome_options}    add_argument    test-type
Call Method    ${chrome_options}    add_argument    --disable-extensions
Call Method    ${chrome_options}    add_argument    --headless
Call Method    ${chrome_options}    add_argument    --disable-gpu
Call Method    ${chrome_options}    add_argument    --no-sandbox
Create Webdriver    Chrome    chrome_options=${chrome_options}

与其

Open Browser    about:blank    headlesschrome
Open Browser    about:blank    chrome

我也遇到了同样的问题,但是针对你上面的评论有点困惑。你能帮忙解释一下吗?我的当前代码是SeleniumLibrary.Open Browser about:blank ${BROWSER},所以我要用你的语句来替换这行代码吗? - Mahak Malik
robotframework-seleniumlimbrary: Open Browser ${url} ${browser} options=add_argument("--no-sandbox") - Petri Ryhänen
robotframework-seleniumlibrary: Open Browser ${url} ${browser} options=add_argument("--no-sandbox") - undefined

5

在我的情况下,错误出现在www-data用户身上,而不是在开发环境中的普通用户身上。这个错误是为这个用户初始化x显示器的问题。因此,问题得以解决,运行我的Selenium测试时不打开浏览器窗口,使用headless模式:

opts.set_headless(True)

4
一个简单的解决方案是,没有其他人提过但对我有用的是,不要在没有使用sudo或者没有以root用户的身份运行。

4
假设您已经下载了chromeDriver,当已经有多个chrome标签页打开时,也会出现此错误。
如果您关闭所有标签页并重新运行,则该错误应该会消失。

2

我也遇到了同样的问题,但是通过以下命令重新安装chrome后问题得到了解决:

$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
$ sudo apt install ./google-chrome-stable_current_amd64.deb

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