简体   繁体   English

Selenium:WebDriverException:Chrome 无法启动:由于 google-chrome 不再运行而崩溃,因此 ChromeDriver 假设 Chrome 已崩溃

[英]Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed

Recently I switched computers and since then I can't launch chrome with selenium.最近我换了电脑,从那以后我不能用 selenium 启动 chrome。 I've also tried Firefox but the browser instance just doesn't launch.我也尝试过 Firefox 但浏览器实例无法启动。

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)

i have the latest chrome version and chromedriver installed我安装了最新的 chrome 版本和 chromedriver

EDIT: After trying @b0sss solution i am getting the following error.编辑:尝试@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)

Try to download HERE and use this latest chrome driver version.尝试在此处下载并使用最新的 chrome 驱动程序版本。

https://sites.google.com/a/chromium.org/chromedriver/downloads https://sites.google.com/a/chromium.org/chromedriver/downloads

EDIT:编辑:

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/PycharmProjects/chromedriver',chrome_options=chrome_options)
d.get('https://www.google.nl/')

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

...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser ie Chrome Browser session. ...暗示ChromeDriver无法启动/生成新的WebBrowser,Chrome 浏览器会话。

Your main issue is the Chrome browser is not installed at the default location within your system.您的主要问题是Chrome浏览器未安装在系统中的默认位置

The server ie ChromeDriver expects you to have Chrome installed in the default location for each system as per the image below:服务器即ChromeDriver希望您按照下图在每个系统的默认位置安装Chrome

Chrome_binary_expected_location

1 For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to be a symlink to the actual Chrome binary. 1对于 Linux 系统,ChromeDriver 期望/usr/bin/google-chrome是指向实际 Chrome 二进制文件的符号链接。


Solution解决方案

In case you are using a Chrome executable in a non-standard location you have to override the Chrome binary location as follows:如果您在非标准位置使用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: 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/");

hope this helps someone.希望这有助于某人。 this worked for me on Ubuntu 18.10这在 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()

I encountered the exact problem running on docker container (in build environment).我遇到了在 docker 容器上运行的确切问题(在构建环境中)。 After ssh into the container, I tried running the test manually and still encountered 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.)

When I tried running chrome locally /usr/bin/google-chrome-stable , error message当我尝试在本地运行 chrome /usr/bin/google-chrome-stable ,错误消息

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

I checked my ChromeOptions and it was missing --no-sandbox , which is why it couldn't spawn chrome.我检查了我的 ChromeOptions 并且它丢失了--no-sandbox ,这就是它无法生成 chrome 的原因。

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

I had a similar issue, and discovered that option arguments must be in a certain order .我有一个类似的问题,并发现选项参数必须按特定顺序 I am only aware of the two arguments that were required to get this working on my Ubuntu 18 machine.我只知道在我的 Ubuntu 18 机器上运行所需的两个参数。 This sample code worked on my end:这个示例代码对我有用:

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

Assuming that you already downloaded chromeDriver, this error is also occurs when already multiple chrome tabs are open.假设您已经下载了 chromeDriver,当已经打开多个 chrome 选项卡时也会出现此错误。

If you close all tabs and run again, the error should clear up.如果关闭所有选项卡并再次运行,错误应该会清除。

in my case, the error was with www-data user but not with normal user on development.在我的情况下,错误出在 www-data 用户身上,而不是在开发中的普通用户身上。 The error was a problem to initialize an x display for this user.该错误是为该用户初始化 x 显示的问题。 So, the problem was resolved running my selenium test without opening a browser window, headless:因此,在不打开浏览器窗口的情况下运行我的 selenium 测试问题解决了,无头:

opts.set_headless(True)

For RobotFramework对于 RobotFramework

I solved it!我解决了! using --no-sandbox使用--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}

Instead of代替

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

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

This error has been happening randomly during my test runs over the last six months (still happens with Chrome 76 and Chromedriver 76) and only on Linux.在我过去六个月的测试运行期间,此错误一直随机发生(Chrome 76 和 Chromedriver 76 仍会发生),并且仅在 Linux 上发生。 On average one of every few hundred tests would fail, then the next test would run fine.平均每几百个测试中有一个会失败,然后下一个测试会运行良好。

Unable to resolve the issue, in Python I wrapped the driver = webdriver.Chrome() in a try..except block in setUp() in my test case class that all my tests are derived from.无法解决该问题,在 Python 中,我将driver = webdriver.Chrome()包装在我的测试用例类中的 setUp() 中的 try..except 块中,我的所有测试均源自该类。 If it hits the Webdriver exception it waits ten seconds and tries again.如果它遇到 Webdriver 异常,它会等待十秒钟并再次尝试。

It solved the issue I was having;它解决了我遇到的问题; not elegantly but it works.不优雅,但它有效。

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)

I came across this error on linux environment.我在 linux 环境中遇到了这个错误。 If not using headless then you will need如果不使用无头,那么你将需要

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

The solutions that every body provide here is good for Clear the face of the issue but每个机构在这里提供的解决方案都有利于清除问题的表面,但是

All you need to solve this problem is that You have to run The App on non-root user on linux.解决这个问题所需要的只是你必须在 linux 上的非 root用户上运行应用程序。

According to this post根据这个帖子

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

I had the same problem but it was solved just by reinstalling chrome again with the commands below:我遇到了同样的问题,但只需使用以下命令重新安装 chrome 即可解决:

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

i had same problem.我有同样的问题。 I was run it on terminal with "sudo geany", you should run it without "sudo" just type on terminal "geany" and it is solved for me.我在终端上用“sudo geany”运行它,你应该在没有“sudo”的情况下运行它,只需在终端“geany”上输入,它就为我解决了。

Make sure that both the chromedriver and google-chrome executable have execute permissions确保chromedrivergoogle-chrome可执行文件都具有执行权限

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

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

i faced the same problem but i solved it by moving the chromedriver to this path '/opt/google/chrome/'我遇到了同样的问题,但我通过将 chromedriver 移动到这个路径“/opt/google/chrome/”来解决它

and this code works correctly并且此代码正常工作

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

In my case, chrome was broken.就我而言,铬坏了。 following two lines fixed the issue,以下两行解决了这个问题,

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

Faced with this issue trying to run/debug Python Selenium script inside WSL2 using Pycharm debugger.面对这个问题,尝试使用 Pycharm 调试器在 WSL2 中运行/调试 Python Selenium 脚本。 First solution was to use --headless mode, but I prefer to have Chrome GUI during the debug process.第一个解决方案是使用--headless模式,但我更喜欢在调试过程中使用 Chrome GUI。

In the system terminal outside Pycharm debugger Chrome GUI worked nice with DISPLAY env variable set this way (followed guide here ):在 Pycharm 调试器外部的系统终端中,Chrome GUI 以这种方式设置DISPLAY env 变量时效果很好(遵循此处的指南):

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

Unfortunately ~/.bashrc is not running in Pycharm during the debug, export is not working.不幸的是~/.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:我从 Pycharm 调试器获得 Chrome GUI 的方式:在 WSL2 中运行echo $DISPLAY ,将 ip(你有类似的东西) 172.18.144.1:0到 Pycharm 调试配置>环境变量中:

在此处输入图片说明

Fixed it buy killing all the chrome processeses running in the remote server before running my script.修复了在运行我的脚本之前杀死远程服务器中运行的所有 chrome 进程的问题。 That may explain why some answers that recommend you run your script as root works.这可能解释了为什么一些建议您以 root 身份运行脚本的答案有效。

$ pkill -9 chrome
$ ./my_script.py

Just do not run the script as the root user (in my case).只是不要以root用户身份运行脚本(在我的情况下)。

Maybe when you where developing in local, you used options.headless=False in order to see what is the browser doing but you forgot to change it to True in the vm.也许当您在本地开发时,您使用 options.headless=False 来查看浏览器在做什么,但您忘记在 vm 中将其更改为 True。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 chromedriver 不再运行,因此 ChromeDriver 假设 Chrome 使用 Selenium 到 Python 发生崩溃错误 - chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed error using Selenium through Python 从 chrome 位置 C:\\..\\Chrome\\Application\\chrome.exe 开始的进程不再运行,因此 ChromeDriver 假设 Chrome 已崩溃 - The process started from chrome location C:\..\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:在 Python 中使用 ChromeDriver 和 Selenium 崩溃 - selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python WebDriverException:消息:未知错误:Chrome 无法启动:在 Amazon Linux 上通过 Selenium Python 使用 ChromeDriver Chrome 出现崩溃错误 - WebDriverException: Message: unknown error: Chrome failed to start: crashed error using ChromeDriver Chrome through Selenium Python on Amazon Linux WebDriverException:消息:服务 /usr/bin/google-chrome 意外退出。 状态代码是:-11 使用 ChromeDriver Chrome 通过 Selenium Python - WebDriverException: Message: Service /usr/bin/google-chrome unexpectedly exited. Status code was: -11 with ChromeDriver Chrome through Selenium Python selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome无法启动:使用ChromeDriver Chrome和Selenium异常退出 - selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium selenium.common.exceptions.WebDriverException:消息:Linux 上的 ChromeDriver Selenium Python 服务 /usr/bin/google-chrome 意外退出 - selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/google-chrome unexpectedly exited with ChromeDriver Selenium Python on Linux WebDriverException:消息:未知错误:Chrome 无法启动:通过 VPS 上的 Python 使用 ChromeDriver Chrome 和 Selenium 异常退出 - WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium through Python on VPS WebDriverException:消息:未知错误:Chrome 无法启动:使用 ChromeDriver Chrome 和 Selenium Python 异常退出错误 - WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally error using ChromeDriver Chrome and Selenium Python WebDriverException:消息:未知错误:Chrome 无法启动:通过 WebDriverManager 使用 Selenium ChromeDriver 和 Chrome 异常退出 - WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally using Selenium ChromeDriver and Chrome through WebDriverManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM