【问题标题】:Selenium does not identify Chrome binary and Chrome has crashedSelenium 无法识别 Chrome 二进制文件,Chrome 已崩溃
【发布时间】:2021-05-26 02:28:40
【问题描述】:

我正在尝试使用 chrome 运行 Selenium

我测试了这个问题:WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome

但它会产生错误:找不到 Chrome 二进制文件

我测试了这个问题:Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed

但它会产生错误:Chrome 已崩溃

机器: Ubuntu 16 和 Python 3.4

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = "/usr/local/bin/chromedriver"
options.add_argument("--start-maximized")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'/usr/local/bin/chromedriver')
driver.get('http://google.com/')

输出

Traceback (most recent call last):
  File "testdrive.py", line 11, in <module>
    driver = webdriver.Chrome(options=options, executable_path=r'/usr/local/bin/chromedriver')
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/local/bin/chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

有人可以帮我吗?

【问题讨论】:

  • 您使用的chrome驱动和chrome浏览器是什么版本的?还有python版本
  • Chrome 版本:91.0.4472.19;版本 Python:3.4

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


【解决方案1】:

自动下载并安装支持当前安装的chrome版本的chromedriver。此安装程序支持 Linux、MacOS 和 Windows 操作系统。

安装

pip install chromedriver-autoinstaller

用法

只需在要使用的模块中输入 import chromedriver_autoinstallerchromedriver

示例

from selenium import webdriver
import chromedriver_autoinstaller


chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists
                                      # and if it doesn't exist, download it automatically,
                                      # then add chromedriver to path

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title

【讨论】:

  • 不工作:sudo pip install chromedriver-autoinstaller,输出:警告:目录'/home/ubuntu/.cache/pip/http'或其父目录不属于当前用户并且缓存已被禁用。请检查该目录的权限和所有者。如果使用 sudo 执行 pip,您可能需要 sudo 的 -H 标志。 |如果我尝试运行,输出: Traceback import chromedriver_autoinstaller, from . import utils, logging.debug(f'Downloading chromedriver ({chromedriver_version})...')
  • 您没有相应的权限
  • 我相信我有权限是的,在消息的末尾它说:要求已经满足:chromedriver-autoinstaller in /usr/local/lib/python3.4/site-packages (0.2.2 )
猜你喜欢
  • 2016-09-19
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 2022-10-03
  • 2012-07-15
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多