Python Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome 不再运行所以 ChromeDriver 假设 Chrome 已经崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53073411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:15:07  来源:igfitidea点击:

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

pythonseleniumgoogle-chromeselenium-webdriverselenium-chromedriver

提问by SOeh

Recently I switched computers and since then I can't launch chrome with selenium. I've also tried Firefox but the browser instance just doesn't launch.

最近我换了电脑,从那以后我就不能用 selenium 启动 Chrome 了。我也试过 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)

回答by NgoCuong

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:

尝试这个:

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

回答by DebanjanB

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 ChromeDriverwas unable to initiate/spawn a new WebBrowseri.e. Chrome Browsersession.

...暗示ChromeDriver无法启动/生成新的WebBrowser,Chrome 浏览器会话。

Your main issue is the Chromebrowser is not installed at the default locationwithin your system.

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

The server i.e. ChromeDriverexpects you to have Chromeinstalled in the default locationfor each system as per the image below:

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

Chrome_binary_expected_location

Chrome_binary_expected_location

1For Linux systems, the ChromeDriver expects /usr/bin/google-chrometo be a symlink to the actual Chrome binary.

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



Solution

解决方案

In case you are using a Chromeexecutable in a non-standard location you have to override the Chrome binary locationas follows:

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

  • PythonSolution:

    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/')
    
  • JavaSolution:

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

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

回答by dataviews

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

回答by Ethan Hill

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. This sample code worked on my end:

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

回答by ibaralf

I encountered the exact problem running on docker container (in build environment). After ssh into the container, I tried running the test manually and still encountered

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

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

回答by Rogelio

in my case, the error was with www-data user but not with normal user on development. The error was a problem to initialize an x display for this user. So, the problem was resolved running my selenium test without opening a browser window, headless:

在我的情况下,错误出在 www-data 用户身上,而不是在开发中的普通用户身上。该错误是为该用户初始化 x 显示的问题。因此,问题已解决,无需打开浏览器窗口即可运行我的 selenium 测试,无头:

opts.set_headless(True)

回答by tugce ozgur oztetik

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.

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

回答by Panupong Kongarn

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

回答by The Stevie T

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. On average one of every few hundred tests would fail, then the next test would run fine.

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

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. If it hits the Webdriver exception it waits ten seconds and tries again.

无法解决这个问题,在 Python 中,我driver = webdriver.Chrome()在我的测试用例类中的 setUp() 中用 try..except 块包装了我的所有测试都来自该类。如果它遇到 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)

回答by Ediz Yurek

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”上输入,它就为我解决了。