【问题标题】:chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed error using Selenium through Pythonchromedriver 不再运行,因此 ChromeDriver 假设 Chrome 已通过 Python 使用 Selenium 崩溃错误
【发布时间】:2022-01-24 16:11:06
【问题描述】:

在我开始之前:我知道有十亿篇关于 Selenium 不起作用的帖子,并且有各种解决方案可以尝试。我相信我已经尝试了一切,但如果我遗漏了什么,请原谅我。我的头撞墙了,希望能得到帮助。

以下是我采取的一些步骤:

我下载了 selenium(Ubuntu、Python)的 chromedriver,并使用 chmod 755chmod 777 使驱动程序可执行。后记,我用./chromedriver启动了chromedriver。

我为 Selenium 尝试了各种选项,包括手动添加运行 chromedriver 的端口

from selenium import webdriver

options = webdriver.ChromeOptions()
options.binary_location = "/home/myname/projects/myproject/chromedriver"
options.add_argument("--remote-debugging-port=9515")
chrome_driver_binary = '/home/myname/projects/myproject/chromedriver'
driver = webdriver.Chrome(chrome_driver_binary, options = options)
driver.get('http://www.ubuntu.com/')

我尝试过其他帖子中建议的选项,例如:

options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--disable-setuid-sandbox")

我已确定我使用的是与我的 Chrome 版本兼容的 chromedriver。

似乎没有任何效果。我不断收到此错误:

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (chrome not reachable)
  (The process started from chrome location /home/myname/projects/myproject/chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

我真诚地感谢别人对这个问题的解释。

【问题讨论】:

  • 我建议使用 headless 选项,并且在一个项目中,我在调用任何网站之前使用了 .implicitly_wait(10) 函数。我也会选择 Chromium 作为驱动程序。
  • 感谢您的回复!我放弃并尝试了 Firefox,这是我第一次尝试。这种情况对我来说仍然是一个谜,我感谢任何人对上述代码中可能有什么问题的意见。

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


【解决方案1】:

您需要注意以下几点:

  • options.binary_location:指的是 二进制位置,如果 Google Chrome 未安装在默认位置,则使用该位置。见:WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome

  • --remote-debugging-port:如果你不是远程调试,你可以安全地放弃这个参数。

  • chrome_driver_binary:指ChromeDriver在系统中的绝对位置。

  • webdriver.Chrome(chrome_driver_binary, options = options):另外你可能想添加 key executable_path 如下:

    chrome_driver_binary = '/home/myname/projects/myproject/chromedriver'
    driver = webdriver.Chrome(executable_path=chrome_driver_binary, options = options)
    driver.get('http://www.ubuntu.com/')
    
  • --no-sandbox--headless--disable-dev-shm-usage--disable-setuid-sandbox等是可选的您可能不需要启动的设置。

发起Selenium驱动ChromeDriver发起的最小代码块浏览上下文可以是:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(executable_path='/home/myname/projects/myproject/chromedriver', options=options)
driver.get("http://www.ubuntu.com/")

Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以 root 用户 (administrator) 运行 Chrome。虽然可以通过在创建 WebDriver 会话时传递 --no-sandbox 标志来解决此问题,但不支持并且强烈建议不要使用此类配置。您需要将环境配置为以普通用户身份运行 Chrome。


参考

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

  • 感谢您如此彻底的回复。
猜你喜欢
  • 2021-12-19
  • 2019-09-01
  • 2019-09-15
  • 1970-01-01
相关资源
最近更新 更多