Python中通过chromedriver设置chrome浏览器

Python中通过chromedriver设置chrome浏览器

本文介绍了在Python中通过chromedriver设置chrome浏览器二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Selenium 与 Python Chrome webdriver 一起使用.在我使用的代码中:

I used Selenium with Python Chrome webdriver.In my code I used:

driver = webdriver.Chrome(executable_path = PATH_TO_WEBDRIVER)

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

to point the webdriver to the webdriver executable. Is there a way to point webdriver to the Chrome Browser binaries?

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

In https://sites.google.com/a/chromium.org/chromedriver/capabilities they have the following (which I assume it what I'm looking for):

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

有人有 Python 的例子吗?

Anyone has an example for Python?

推荐答案

您可以通过 ChromeDriver 使用 Python 使用以下不同方式设置 Chrome 浏览器二进制位置:

You can set Chrome Browser Binary location through ChromeDriver using Python ing the following different ways:

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')

这篇关于在Python中通过chromedriver设置chrome浏览器二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:44