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

Sou*_*ers 8 selenium google-chrome python-3.x selenium-chromedriver google-chrome-headless

我试图搜索,但还没有找到明确的答案。在没有实际安装 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)
Run Code Online (Sandbox Code Playgroud)

问题:除了 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)
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 7

这个错误信息...

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)
Run Code Online (Sandbox Code Playgroud)

...暗示ChromeDriver在尝试启动新的浏览上下文(Chrome 浏览器会话)时无法找到Chrome 二进制文件


根据ChromeDriver wiki 页面中的文档

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

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

    • 有浏览器本身,即chrome
    • Selenium 项目提供的语言绑定,即驱动程序
    • 可执行从下载Chromium项目充当之间的桥梁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 二进制文件的符号链接。

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


解决方案

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


参考

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


Sou*_*ers 4

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

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


归档时间:

查看次数:

11553 次

最近记录:

5 年,2 月 前