首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Brave浏览器中使用Selenium传递用python编写的服务对象

在Brave浏览器中使用Selenium传递用python编写的服务对象
EN

Stack Overflow用户
提问于 2021-11-15 06:36:37
回答 2查看 311关注 0票数 0

#TLDR我想使用带有用python编写的selenium的勇敢浏览器,但找不到任何当前可用的解决方案。

这段代码可以工作

代码语言:javascript
运行
复制
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已被弃用:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
# 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上使用当前的代码(请阅读非弃用的代码)吗?耽误您时间,实在对不起。

EN

回答 2

Stack Overflow用户

发布于 2021-11-15 06:48:28

你必须将你的路径设置为勇敢的二进制。

代码语言:javascript
运行
复制
options.setBinary("Path to brave.exe");

请访问以下网站:

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

票数 0
EN

Stack Overflow用户

发布于 2021-11-15 21:44:27

要启动brave浏览上下文,需要执行以下操作:

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

代码块:

代码语言:javascript
运行
复制
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")

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

参考文献

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69970256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档