使用 Selenium 时需要安装 Chrome 还是只需要安装 chromedriver?

Is Chrome installation needed or only chromedriver when using Selenium?

我试过搜索,但没有找到明确的答案。在 Windows Server 2016 上实际安装 Chrome 浏览器。我下载了正确的 "chromedriver.exe" 并将其放在 "D:\Apps\chromedriver.exe" 中。我已将完整路径添加到我的环境 PATH "D:\Apps\chromedriver.exe".

当我尝试启动使用最新 Selenium 的 Windows 服务时,出现以下错误:

Exception occurred: Failed initializing web driver: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)

问题:除了 chromedriver 之外,我是否必须实际安装功能完善的浏览器,或者这只是在我的 Python 代码中找不到 chromedriver.exe(包含在下面的完整代码中)披露):

def __init__(self, username, password, environment='cert'):
    self.username = username
    self.password = password
    self.environment = environment

    # Instantiate a chrome options object so you can set the size and headless preference
    self.chrome_options = Options()

    # Toggle Headless or not
    if HEADLESS_TOGGLE == 1:
        self.chrome_options.add_argument("--headless")

    self.chrome_options.add_argument("--disable-gpu")  # Disables "Lost UI Shared Context GPU Error on Windows"
    self.chrome_options.add_argument('--disable-extensions')  # Disables Extensions
    self.chrome_options.add_argument("--disable-software-rasterizer")  # Disables "Lost UI Shared Context GPU Error on Windows"
    self.chrome_options.add_argument("--window-size=1024x768")
    self.chrome_options.add_argument("--log-level=3")  # Errors Only
    self.chrome_options.add_argument("--incognito")  # Keeps history and logs clear
    self.chrome_options.add_argument("--no-sandbox")
    self.chrome_options.add_argument("--mute_audio")  # No loud surprises!
    self.chrome_options.add_argument("--no-gpu")  # Disables gpu-based errors (headless)

    self.driver = webdriver.Chrome(chrome_options=self.chrome_options)

用户提供了相关 link 以确认,"YES" 除了实际的 chromedriver 之外还需要完整的 Chrome 安装。

Link: https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver

这个错误信息...

Exception occurred: Failed initializing web driver: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)

...意味着 ChromeDriver 无法找到 Chrome 二进制文件 同时尝试启动新的 浏览上下文 Chrome 浏览器 session.


根据 ChromeDriver 的维基页面中的文档:

  • ChromeDriver is a standalone server which earlier implemented the WebDriver's wire protocol but slowly and gradually shifting it's implementation as per WebDriver standard.

  • ChromeDriver由三部分组成。

    • 有浏览器本身,即 chrome
    • Selenium 项目提供的语言绑定,即 driver
    • Chromium project 下载的可执行文件,充当 chromedriver 之间的桥梁,称为 chromedriver 我们将其称为 server.
  • 在一般情况下,server 希望您在每个系统的默认位置安装 Chrome
    • Linux: /usr/bin/google-chrome 1
    • Mac: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
    • Windows XP: %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
    • Windows Vista 和更高版本: C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe

注意1:对于Linux系统,ChromeDriver期望/usr/bin/google-chrome 成为实际 Chrome 二进制文件的符号链接。

You can find a detailed discussion on overriding the default Chrome binary location in WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome


解决方案

所以理想情况下使用 ChromeDriver / Chrome 组合执行测试需要:


参考

您可以在以下位置找到详细讨论:

就我而言,我使用的是 PowerShell。我发现你需要根据安装的 Chrome 浏览器,将正确的 chromedriver.exe 版本放在 Selenium 模块安装文件夹下的路径中,并替换现有文件。

查看此答案了解详情: