#TLDR 我想使用勇敢的浏览器和用 python 编写的 selenium,但找不到任何当前有效的解决方案。
这段代码有效
from selenium import webdriver
option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files\BraveSoftware\Brave-
Browser\Application\brave.exe'
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe',
options=option)
driver.get("https://www.google.com")
driver.quit()
但executable_path已被弃用:
C:\Users\USER\PycharmProjects\pythonProject\sol2.py:5:
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', options=option)
在 youtube 上找到了这个:https://www.youtube.com/watch?v=VMzmVFA-Gps https://www.youtube.com/watch?v=VMzmVFA-Gps
# import statements
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# Declare variables and setup services
driverService = Service('C:/webdrivers/chromedriver.exe')
# 1. Passes the chromedriver path to the service object
# 2. stores the service object in the s variable
driver = webdriver.Chrome(service=driverService)
# 1. Passes service object driverSerice into the webdriver.Chrome
# 2. Stores object in driver variable
# Body (actually doing stuff)
driver.maximize_window() # maximizes the browser window
driver.get("https://www.google.com") # navigates to google.com
myPageTitle = driver.title
# gets the title of the web page stores in myPageTitle
print(myPageTitle) # prints myPageTitle to Console
assert "Google" in myPageTitle
# checks myPageTitle to ensure it contains Google
# clean up
driver.quit() # closes the browser
当我运行这段代码时,我得到:
selenium.common.exceptions.WebDriverException:消息:未知错误:找不到 Chrome 二进制文件
只要您允许在您的电脑上使用 Google Chrome,此代码就可以工作。我不想在我的电脑上安装 Chrome。
问题是我不知道如何让selenium 使用brave 而不是Chrome。
截至撰写本文时,我正在使用以下内容:
Windows 11 家庭版
硒 v4.0.0
Python v3.10
Chrome驱动程序95.0.4638.69
Brave 浏览器版本 1.31.91 Chromium:95.0.4638.69(官方版本)(64 位)
有人可以解释一下如何在勇敢的浏览器上使用当前(阅读未弃用的)代码来使其工作吗?谢谢你的时间。