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

新手上路,请多包涵

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

原文由 SOeh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

尝试在此处下载并使用最新的 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/')

原文由 NgoCuong 发布,翻译遵循 CC BY-SA 4.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 ,即 Chrome 浏览器 会话。

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

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

Chrome_binary_expected_location

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


解决方案

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

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

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题