【问题标题】:Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashedSelenium:WebDriverException:Chrome 无法启动:由于 google-chrome 不再运行而崩溃,因此 ChromeDriver 假设 Chrome 已崩溃
【发布时间】:2019-04-04 00:46:44
【问题描述】:

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

【问题讨论】:

    标签: python selenium google-chrome selenium-webdriver selenium-chromedriver


    【解决方案1】:

    尝试在此处下载并使用最新的 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/<user>/chromedriver',chrome_options=chrome_options)
    d.get('https://www.google.nl/')
    

    【讨论】:

    • 我已经从那个位置下载了最新的 chromedriver。
    • 确保--no-sandbox 选项是第一个(与此示例不同,第一个选项是--headless。我一直收到该错误,直到我将--no-sandbox 移到顶部列表,将其添加为第一个选项。
    • 注:from selenium.webdriver.chrome.options import Options
    • 据我所知--no-sandbox chrome options 参数就足够了
    • 你需要匹配你使用的chromedriver版本
    【解决方案2】:

    此错误消息...

    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 无法启动/生成新的 WebBrowserChrome 浏览器 会话。

    您的主要问题是 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/')
      
    • Java 解决方案:

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

    【讨论】:

    • 设置 chrome 二进制文件有效。给你最好的答案。也完全删除 Chrome/Chromedriver/Pycharm 然后重新安装所有东西也可以解决问题,所以现在我不会有设置的 chrome 二进制文件。
    • selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:被杀死。 (未知错误:DevToolsActivePort 文件不存在)我现在得到的是这个(killed)而不是crashed。我已经在网上查找了 3 个多小时,但似乎没有任何效果
    【解决方案3】:

    假设你已经下载了chromeDriver,当已经打开了多个chrome标签时也会出现这个错误。

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

    【讨论】:

      【解决方案4】:

      我遇到了在 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) }
      )
      

      【讨论】:

      • 解决此问题的另一种方法是不以 root 身份运行进程(在容器内)。无论如何,从安全角度来看,这更好。 :)
      【解决方案5】:

      在我的情况下,错误是与 www-data 用户有关,但与开发中的普通用户无关。错误是为此用户初始化 x 显示的问题。因此,在不打开浏览器窗口的情况下运行我的 selenium 测试的问题得到了解决,无头:

      opts.set_headless(True)
      

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          我遇到了类似的问题,发现选项参数必须按特定顺序。我只知道在我的 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/')
          

          【讨论】:

          • 不是,使用 --no--sandbox 选项存在安全问题吗?
          【解决方案8】:

          在我过去六个月的测试运行期间,此错误一直随机发生(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 正常。
          • capabilities 未定义
          【解决方案9】:

          对于机器人框架

          我解决了!使用--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} ..所以我用你的语句替换这一行?
          【解决方案10】:

          我有同样的问题。我用“sudo geany”在终端上运行它,你应该在没有“sudo”的情况下运行它,只需在终端上输入“geany”就可以了。

          【讨论】:

            【解决方案11】:

            一个没有人说过但对我有用的简单解决方案是没有sudo 或不以root 身份运行。

            【讨论】:

              【解决方案12】:

              确保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"
              【解决方案13】:

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

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

              【讨论】:

                【解决方案14】:

                我遇到了同样的问题,但我通过将 chromedriver 移动到此路径来解决它 '/opt/google/chrome/'

                这段代码可以正常工作

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

                【讨论】:

                  【解决方案15】:

                  每个人在这里提供的解决方案都有助于解决问题,但是

                  你需要解决这个问题是你必须在 non-root 用户上运行应用程序 在 Linux 上。

                  根据这篇文章

                  https://github.com/paralelo14/google_explorer/issues/2#issuecomment-246476321

                  【讨论】:

                    【解决方案16】:

                    就我而言,chrome 坏了。以下两行解决了这个问题,

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

                    【讨论】:

                      【解决方案17】:

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

                      在 Pycharm 调试器之外的系统终端中,Chrome GUI 与 DISPLAY 以这种方式设置的环境变量运行良好(遵循指南 here):

                      export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0

                      很遗憾,~/.bashrc 在调试期间没有在 Pycharm 中运行,导出不起作用。

                      我从 Pycharm 调试器获得 Chrome GUI 的方式:在 WSL2 中运行 echo $DISPLAY,将 ip(你有类似的东西)172.18.144.1:0 粘贴到 Pycharm 调试配置 > 环境变量中:

                      【讨论】:

                      【解决方案18】:

                      修复了它在运行我的脚本之前杀死远程服务器中运行的所有 chrome 进程。 这可以解释为什么一些建议您以 root 身份运行脚本的答案有效。

                      $ pkill -9 chrome
                      $ ./my_script.py
                      

                      【讨论】:

                        【解决方案19】:

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

                        【讨论】:

                          最近更新 更多