Python Selenium:WebDriverException:Chrome启动失败:崩溃,因为google Chrome不再运行,所以ChromeDriver假设Chrome已经崩溃

Python Selenium:WebDriverException:Chrome启动失败:崩溃,因为google Chrome不再运行,所以ChromeDriver假设Chrome已经崩溃,python,selenium,google-chrome,selenium-webdriver,selenium-chromedriver,Python,Selenium,Google Chrome,Selenium Webdriver,Selenium Chromedriver,最近我换了电脑,从那以后我就不能用selenium启动chrome了。我也尝试过Firefox,但浏览器实例无法启动 from selenium import webdriver d = webdriver.Chrome('/home/PycharmProjects/chromedriver') d.get('https://www.google.nl/') 我得到以下错误: selenium.common.exceptions.WebDriverException: Message: u

最近我换了电脑,从那以后我就不能用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)

请尝试在此处下载并使用此最新的chrome驱动程序版本

编辑:

试试这个:

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/PycharmProjects/chromedriver',chrome_options=chrome_options)
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.)
…表示ChromeDriver无法启动/生成新的WebBrowser,即Chrome浏览器会话

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

服务器即ChromeDriver希望您在每个系统的默认位置安装Chrome,如下图所示:

1对于Linux系统,ChromeDriver希望
/usr/bin/google chrome
是实际chrome二进制文件的符号链接


解决方案 如果在非标准位置使用Chrome可执行文件,则必须按如下方式覆盖Chrome二进制位置:

  • Python解决方案:

    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/')
    
    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/");
    
  • Java解决方案:

    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/')
    
    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/");
    

假设您已经下载了chromeDriver,当已经有多个chrome选项卡打开时,也会发生此错误


如果关闭所有选项卡并再次运行,则错误应清除

我在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,发现它丢失了
——没有沙盒,这就是它无法生成chrome的原因

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

在我的例子中,错误发生在www数据用户身上,而不是开发中的普通用户身上。该错误是为该用户初始化x显示时出现的问题。因此,在不打开浏览器窗口的情况下运行我的selenium测试就解决了问题,headless:

opts.set_headless(True)

希望这对别人有帮助。这在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()

我也遇到了类似的问题,并发现选项参数必须按特定顺序排列。我只知道在我的Ubuntu18机器上运行这个程序需要两个参数。此示例代码在我这方面起作用:

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/')

在过去六个月的测试运行期间,这个错误一直是随机发生的(Chrome 76和Chromedriver 76仍然会发生),并且仅在Linux上发生。平均每几百个测试中就有一个会失败,然后下一个测试就会正常运行

无法解决此问题,在Python中,我将
driver=webdriver.Chrome()
包装在一个try中。除了测试用例类中的setUp()中的block之外,我的所有测试都是从中派生出来的。如果遇到Webdriver异常,它将等待10秒钟,然后重试

它解决了我的问题;不优雅,但它的工作

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)
机器人框架

我解决了!使用
--无沙箱

${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

我也有同样的问题。我是用“sudo geany”在终端上运行它的,你应该在没有“sudo”的情况下运行它,只需在终端上键入“geany”,它就为我解决了

一个没有人说过但对我有用的简单解决方案不是在没有
sudo
或不是root的情况下运行的。

确保
chromedriver
google chrome
可执行文件都具有执行权限

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

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

我在linux环境中遇到了这个错误。如果不使用无头,则需要

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

我遇到了同样的问题,但我通过将chromedriver移到这条路径来解决它 “/opt/google/chrome/”

这段代码工作正常

from selenium.webdriver import Chrome
driver = Chrome('/opt/google/chrome/chromedrive')
driver.get('https://google.com')

每个机构在这里提供的解决方案都有助于澄清问题的真相,但是

您只需在非root用户上运行应用程序即可解决此问题 在linux上

根据这篇文章


在我的情况下,铬被破坏了。下面两行代码解决了这个问题

apt -y update; apt -y upgrade; apt -y dist-upgrade
apt --fix-broken install

在尝试使用Pycharm调试器在WSL2内部运行/调试Python Selenium脚本时遇到此问题。 第一个解决方案是使用
--headless
模式,但我更喜欢在调试过程中使用ChromeGUI

在Pycharm调试器外部的系统终端中,Chrome GUI以这种方式设置
DISPLAY
env变量,效果良好(遵循指南):

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调试配置>环境变量中:


我已经从该位置下载了最新的chromedriver。请确保第一个选项是
--无沙盒
(与此示例不同,第一个选项是
--headless
。我一直收到该错误,直到我将
--no sandbox
移动到列表顶部,将其添加为第一个选项。注意:
来自selenium.webdriver.chrome.options导入选项据我所知
--no sandbox
chrome options参数不适合您。)需要匹配chromedriver的版本,你使用Chromium设置chrome二进制文件有效。给了你最好的答案。同时完全删除chrome/chromedriver/Pycharm,然后重新安装一切也起到了作用,所以现在我不会设置chrome二进制文件。另一种解决方法是不运行该进程(在容器中)作为root。无论如何,从安全角度来看,这更好。:)我也面临着同样的问题,但有点欺骗