【问题标题】:Set chrome browser binary through chromedriver in Python在 Python 中通过 chromedriver 设置 chrome 浏览器二进制文件
【发布时间】:2018-01-12 00:38:18
【问题描述】:

我将 Selenium 与 Python Chrome 网络驱动程序一起使用。 在我使用的代码中:

driver = webdriver.Chrome(executable_path = PATH_TO_WEBDRIVER)

将 webdriver 指向 webdriver 可执行文件。有没有办法将 webdriver 指向 Chrome 浏览器二进制文件?

https://sites.google.com/a/chromium.org/chromedriver/capabilities 他们有以下内容(我认为这是我正在寻找的):

ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");

谁有 Python 的例子?

【问题讨论】:

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


    【解决方案1】:

    首先,如果你想使用 chrome,那么你需要从下面的 URL 下载它的二进制文件:-

    https://sites.google.com/a/chromium.org/chromedriver/
    

    现在您需要将此驱动程序路径传递给 selenium webdriver。

    如果您使用的是 python,代码应该如下所示:-

        driver = webdriver.Chrome('C:\Users\name\Downloads\chromedriver_win32 (3)\chromedriver.exe')
        driver.implicitly_wait(30) # seconds
        driver.get('https://www.google.co.in/')
    

    希望对你有帮助:)

    【讨论】:

    • 其实这不是我要找的。 Chromedriver 二进制文件和 Chrome 浏览器二进制文件是两个不同的东西。对于 Chromedriver,我已经有一个工作代码。我指的是 Chrome 浏览器二进制文件。
    【解决方案2】:

    您可以通过 ChromeDriver 使用 Python 设置 Chrome 浏览器二进制位置,方法如下:


    使用选项

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    options = Options()
    options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
    driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Utility/BrowserDrivers/chromedriver.exe", )
    driver.get('http://google.com/')
    

    使用 DesiredCapabilities

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    cap = DesiredCapabilities.CHROME
    cap = {'binary_location': "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}
    driver = webdriver.Chrome(desired_capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
    driver.get('http://google.com/')
    

    使用 Chrome 即服务

    from selenium import webdriver
    import selenium.webdriver.chrome.service as service
    service = service.Service('C:\\Utility\\BrowserDrivers\\chromedriver.exe')
    service.start()
    capabilities = {'chrome.binary': "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}
    driver = webdriver.Remote(service.service_url, capabilities)
    driver.get('http://www.google.com')
    

    【讨论】:

    • 看起来方向正确。我正在使用 mac。我尝试下载 chrome 浏览器二进制文件,但我发现的唯一内容是 Chromium 二进制文件,如果我尝试使用它,驱动程序将出错“未找到 chrome 二进制文件”。另一方面,我尝试从 /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome 移动 chrome 并在本地使用它,但驱动程序只是挂起。
    • @user123 从正确的位置获取 ChromeDriver https://sites.google.com/a/chromium.org/chromedriver 谢谢
    • 我在 ChromeOptions 文档中看到了 binary_location,所以我接受了答案。现在我仍然坚持为 Mac 找到合适的 chrome 可执行文件。
    • 我只看到 chromedriver 下载而不是实际的 chrome 浏览器
    • @user123 如果我没记错的话,您想使用多个版本的 Chrome 浏览器版本吗?然后你只需要从 Chromium 网站下载我想。我用 Mozilla Firefox 做同样的事情:)
    【解决方案3】:

    非常感谢,我为此苦苦挣扎了 2.5 小时,因为我不知道如何在 Python 中设置 Chrome 可执行文件路径。现在工作

    options = Options()
    options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
    driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Utility/BrowserDrivers/chromedriver.exe", )
    

    【讨论】:

      【解决方案4】:

      有没有办法将 webdriver 指向 Chrome 浏览器二进制文件?

      正如其他人已经说过的,使用binary_location。但是,Chrome 的位置会根据平台移动。 Fedora 和 Ubuntu 使用不同的位置。所以你可能想使用类似的东西:

      def get_chrome():
          if os.path.isfile('/usr/bin/chromium-browser'):
              return '/usr/bin/chromium-browser'
          elif os.path.isfile('/usr/bin/chromium'):
              return '/usr/bin/chromium'
          elif os.path.isfile('/usr/bin/chrome'):
              return '/usr/bin/chrome'
          elif os.path.isfile('/usr/bin/google-chrome'):
              return '/usr/bin/google-chrome'
          else:
              return None
      

      然后:

      if version.parse(selenium.__version__) >= version.parse("3.0"):
          opts = Options()
          opts.binary_location = get_chrome()
          opts.add_argument('--headless')
          opts.add_argument('--no-sandbox')
          opts.add_argument('--disable-dev-shm-usage')
      
          driver = webdriver.Chrome(chrome_options=opts)
          driver.maximize_window()
      else:
          opts = Options()
          opts.headless = True
          opts.binary_location = get_chrome()
      
          driver = webdriver.Chrome(chrome_options=opts)
          driver.maximize_window()
      
      agent = driver.execute_script('return navigator.userAgent')
      

      【讨论】:

        猜你喜欢
        • 2019-01-26
        • 2019-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多