使用 Selenium 时是否需要安装 Chrome 或仅安装 chromedriver?

新手上路,请多包涵

我试图搜索,但没有找到明确的答案。在没有实际安装 Chrome 浏览器的 Windows Server 2016 上。我下载了正确的“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)

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

阅读 2.2k
2 个回答

用户提供了相关链接以确认“是”除了实际的 chromedriver 之外还需要完整的 Chrome 安装。

链接: https ://github.com/SeleniumHQ/selenium/wiki/ChromeDriver

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

这个错误信息…

 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 二进制文件_。


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

  • ChromeDriver 是一个独立的服务器,它较早地实现了 WebDriver有线协议,但根据 WebDriver 标准 慢慢地逐渐改变它的实现。

  • ChromeDriver 由三个独立的部分组成。

    • 有浏览器本身,即 chrome
    • Selenium 项目提供的语言绑定即 驱动程序
    • An executable downloaded from the Chromium project which acts as a bridge between chrome and the driver which is called chromedriver and we refer to it as the 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 二进制文件的符号链接。

您可以在 WebDriverException: unknown error: cannot find Chrome binary error with Selenium in Python for older versions of Google Chrome 中 找到关于覆盖默认 Chrome 二进制位置 的详细讨论


解决方案

因此,理想情况下,要使用 ChromeDriver / Chrome 组合执行测试,您需要:


参考

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

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

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