在Python中通过chromedriver设置chrome浏览器二进制文件

use*_*123 14 python linux selenium google-chrome selenium-webdriver

我使用Selenium和Python Chrome webdriver.在我使用的代码中:

driver = webdriver.Chrome(executable_path = PATH_TO_WEBDRIVER)
Run Code Online (Sandbox Code Playgroud)

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

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

ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");
Run Code Online (Sandbox Code Playgroud)

有人有Python的例子吗?

Deb*_*anB 19

您可以通过以下方式在Chrome中将Chrome浏览器二进制文件设置为chrome webdriver:


使用Options班级:

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

使用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/')
Run Code Online (Sandbox Code Playgroud)

使用Chrome作为Service:

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