Selenium:WebDriverException:Chrome未能启动:崩溃了,因为google-chrome不再运行了,所以ChromeDriver认为Chrome已经崩溃了。

92 人关注

最近我换了电脑,从那时起我就不能用Selenium启动chrome了。我也试过火狐,但浏览器实例就是不启动。

from selenium import webdriver
d = webdriver.Chrome('/home/PycharmProjects/chromedriver')
d.get('https://www.google.nl/')

i get the following error:

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。

EDIT: 在尝试了@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
SOeh
SOeh
发布于 2018-10-31
21 个回答
NgoCuong
NgoCuong
发布于 2020-06-23
已采纳
0 人赞同

尝试在这里下载并使用这个最新的chrome驱动版本。

  • https://sites.google.com/chromium.org/driver/
  • Try this:

    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/')
        
    确保 --no-sandbox 选项是第一个(与此例不同,第一个选项是 --headless 。我一直得到这个错误,直到我把 --no-sandbox 移到列表的顶部,把它作为第一个选项。
    Note: from selenium.webdriver.chrome.options import Options
    据我所知 --no-sandbox chrome选项参数就足够了。
    你需要将你使用的chromedriver的版本与chromium匹配。
    @parsecer 你的评论拯救了我。非常感谢你。
    undetected Selenium
    undetected Selenium
    发布于 2020-06-23
    0 人赞同

    这个错误信息...

    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 i.e. Chrome Browser session.

    你的主要问题是Chrome浏览器没有安装在默认位置 within your system.

    The server i.e. ChromeDriver希望你能有Chrome安装在默认位置如下图所示,每一个系统都有一个 "小 "字。

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

    Solution

    如果你使用的是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/')
    
  • 爪哇 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/");
        
  • SOeh
    设置chrome二进制的工作。给了你最好的答案。另外,完全删除Chrome/Chromedriver/Pycharm,然后重新安装一切,也起到了作用,所以现在我不会有设置Chrome二进制的问题。
    selenium.common.exceptions.WebDriverException。消息:未知错误。Chrome启动失败:被杀死。 (未知错误:DevToolsActivePort文件不存在)我现在得到的是这个( killed )而不是 crashed 。我已经在网上查找了3个多小时,但似乎都没有用。
    dataviews
    dataviews
    发布于 2020-06-23
    0 人赞同

    hope this helps someone. this worked for me on 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()
        
    ibaralf
    ibaralf
    发布于 2020-06-23
    0 人赞同

    我遇到了在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身份运行进程(在容器内)。无论如何,从安全的角度来看,这样做会更好。)
    Ethan Hill
    Ethan Hill
    发布于 2020-06-23
    0 人赞同

    我有一个类似的问题,发现 选项参数必须按一定顺序排列 .我只知道在我的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 选项不是一个安全问题吗?
    Panup Pong
    Panup Pong
    发布于 2020-06-23
    0 人赞同

    对于RobotFramework

    I solved it! using --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}。 ...所以我用你的语句替换这一行?
    tugce ozgur oztetik
    tugce ozgur oztetik
    发布于 2020-06-23
    0 人赞同

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

    如果你关闭所有标签并再次运行,错误应该会消除。

    0 人赞同

    在我的例子中,错误发生在www-data用户身上,而不是开发中的普通用户。这个错误是为这个用户初始化一个X显示的问题。所以,在不打开浏览器窗口的情况下运行我的Selenium测试,问题就解决了,无头的。

    opts.set_headless(True)
        
    Carter
    Carter
    发布于 2020-06-23
    0 人赞同

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

    The Stevie T
    The Stevie T
    发布于 2020-06-23
    0 人赞同

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

    由于无法解决这个问题,我在Python中把 driver = webdriver.Chrome() 包装在我的测试用例类的setUp()中的try...except块中,我的所有测试都来自这个类。如果它遇到了Webdriver的异常,它会等待10秒,然后再试一次。

    它解决了我所遇到的问题;虽然不优雅,但它是有效的。

    from selenium import webdriver
    from selenium.common.exceptions import WebDriverException
        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)
        
    I see this error with selenium==4.0.0.b1 selenium==4.0.0.a7 works fine, only in linux, windows fine.
    capabilities is not defined
    Kevin Smeeks
    Kevin Smeeks
    发布于 2020-06-23
    0 人赞同

    我在linux环境下遇到了这个错误。如果不使用headless,那么你将需要

    from sys import platform
        if platform != 'win32':
            from pyvirtualdisplay import Display
            display = Display(visible=0, size=(800, 600))
            display.start()
        
    Soroosh Gholami
    Soroosh Gholami
    发布于 2020-06-23
    0 人赞同

    每个人在这里提供的解决方案都是很好的,可以清除问题的表面,但

    你需要解决这个问题的是,你必须在以下平台上运行该应用程序 non-root user on linux.

    根据这个帖子

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

    klapshin
    klapshin
    发布于 2020-06-23
    0 人赞同

    在使用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中运行,导出也不工作。

    The way I've got Chrome GUI worked from Pycharm debugger: run echo $DISPLAY in WSL2, paste ip (you've got something similar to this) 172.18.144.1:0 into Pycharm Debug Configuration > Environment Variables:

    在克隆问题中发现了类似的解决方案,这也可能有助于 stackoverflow.com/a/57403093/6875391
    Vala Khosravi
    Vala Khosravi
    发布于 2020-06-23
    0 人赞同

    我也有同样的问题,但只是通过重新安装chrome和下面的命令就解决了。

    $ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    $ sudo apt install ./google-chrome-stable_current_amd64.deb
        
    Ediz Yurek
    Ediz Yurek
    发布于 2020-06-23
    0 人赞同

    我也有同样的问题。我是用 "sudo geany "在终端上运行的,你应该不用 "sudo",只需在终端上输入 "geany "就可以解决了。

    Ahmed Al-yamany
    Ahmed Al-yamany
    发布于 2020-06-23
    0 人赞同

    我也遇到了同样的问题,但我通过将chromedriver移到这个路径上解决了这个问题 '/opt/google/chrome/'

    而这段代码可以正常工作

    from selenium.webdriver import Chrome
    driver = Chrome('/opt/google/chrome/chromedrive')
    driver.get('https://google.com')
        
    Dileep
    Dileep
    发布于 2020-06-23
    0 人赞同

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

    apt -y update; apt -y upgrade; apt -y dist-upgrade
    apt --fix-broken install
        
    0n10n_
    0n10n_
    发布于 2020-06-23
    0 人赞同

    在运行我的脚本之前,我买下了在远程服务器上运行的所有chrome进程,并将其修复。 这可能解释了为什么有些答案建议你以root身份运行你的脚本。

    $ pkill -9 chrome
    $ ./my_script.py
        
    AMMAR ELHAMDO
    AMMAR ELHAMDO
    发布于 2020-06-23
    0 人赞同

    只是不要将该脚本作为 root 用户(就我而言)。

    Fabrizio
    Fabrizio
    发布于 2020-06-23
    0 人赞同

    也许当你在本地开发时,你使用了options.headless=False,以便看到浏览器在做什么,但你忘了在虚拟机中把它改为True。

    这并没有提供问题的答案。一旦你有足够的 声誉 你将能够 评论任何帖子 而不是。 提供不需要提问者澄清的答案 . - From Review
    Ashlesha Waikos
    Ashlesha Waikos
    发布于 2020-06-23
    0 人赞同