#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
# 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 s 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主页
Selenium v4.0.0
Python v3.10
ChromeDriver 95.0.4638.69
Brave Browser Version 1.31.91 Chromium: 95.0.4638.69 (官方版本)(64位)
有人能解释一下如何在brave browser上使用当前的代码(请阅读非弃用的代码)吗?耽误您时间,实在对不起。
发布于 2021-11-15 06:48:28
你必须将你的路径设置为勇敢的二进制。
options.setBinary("Path to brave.exe");
请访问以下网站:
https://mundrisoft.com/tech-bytes/how-to-execute-selenium-script-in-brave-browser/
发布于 2021-11-15 21:44:27
要启动brave浏览上下文,需要执行以下操作:
binary_location
属性指向勇敢的二进制位置。代码块:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe'
driverService = Service('C:/Users/.../chromedriver.exe')
driver = webdriver.Chrome(service=driverService, options=option)
driver.get("https://www.google.com")
注意:是一条无害的警告消息,不会影响您的测试执行,您仍然可以忽略它。
参考文献
您可以在以下位置找到几个相关的详细讨论:
https://stackoverflow.com/questions/69970256
复制相似问题