0

#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: Message: unknown error: cannot find Chrome binary

只要您允许在您的 PC 上使用 Google Chrome,此代码就可以工作。我不想在我的电脑上安装 Chrome。

问题是我不知道如何让硒使用勇敢而不是铬。

在撰写本文时,我正在使用以下版本:
Windows 11 Home
Selenium v​​4.0.0
Python v3.10
ChromeDriver 95.0.4638.69
Brave Browser Version 1.31.91 Chromium: 95.0.4638.69 (Official Build) (64-bit)

有人可以解释一下如何在勇敢的浏览器上使用当前的(阅读不推荐的)代码来实现这项工作吗?谢谢你的时间。

4

2 回答 2

0

您必须设置通往勇敢二进制的道路。

options.setBinary("Path to brave.exe");

浏览这个网站:

https://mundrisoft.com/tech-bytes/how-to-execute-selenium-script-in-brave-browser/

于 2021-11-15T06:48:27.643 回答
0

要启动的浏览上下文,您需要:

  • 使用binary_location属性指向勇敢的二进制位置。
  • 使用chromedriver可执行文件启动勇敢的浏览器。

代码块:

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

注意:DeprecationWarning: executable_path has been deprecated是一条无害的警告消息,不会影响您的测试执行,您仍然可以忽略它。


参考

您可以在以下位置找到一些相关的详细讨论:

于 2021-11-15T21:44:26.583 回答