我从谷歌的Chrome切换到Brave网络浏览器,很难让它像Chrome一样与Brave一起工作。勇敢是基于铬的,所以我猜它应该不会那么难。我确保我的 Brave 和 Chromedriver 在这样的同一版本上,
~/some/path $ chromedriver --version
ChromeDriver 76.0.3809.126 (d80a294506b4c9d18015e755cee48f953ddc3f2f-refs/branch-heads/3809@{#1024})
我的chromedriver也在/user/bin
,
~/path $ cd /usr/bin/
/usr/bin $ ls | grep chromedriver
chromedriver
为了检查勇敢的版本,我得到:Version 0.68.132 Chromium: 76.0.3809.132 (Official Build) (64-bit)
然后我运行这段代码,
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/usr/bin/brave-browser')
driver.get("http://www.python.org")
driver.close()
这将打开一个 Brave 窗口,但随后不是获取驱动程序指向的页面,而是引发异常,
Traceback (most recent call last):
File "webscrap.py", line 3, in <module>
driver = webdriver.Chrome(executable_path='/usr/bin/brave-browser')
File "/home/username/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/home/username/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/home/username/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/brave-browser unexpectedly exited. Status code was: -11
我终于设法让它工作了:
试试这个python脚本(python3.7(
from selenium import webdriver
driver_path = "C:/Users/username/PycharmProjects/chromedriver.exe"
brave_path = "C:/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe"
option = webdriver.ChromeOptions()
option.binary_location = brave_path
# option.add_argument("--incognito") OPTIONAL
# option.add_argument("--headless") OPTIONAL
# Create new Instance of Chrome
browser = webdriver.Chrome(executable_path=driver_path, chrome_options=option)
browser.get("https://www.google.es")
干杯。
executable_path
键用于传递 WebDriver 二进制文件的绝对路径,即chromedriver可执行文件。
要启动Brave浏览器会话,您必须通过ChromeOptions
实例的binary_location
参数传递brave 浏览器二进制文件的绝对位置。
因此,有效的代码块将是:
from selenium import webdriver
chromedriver_path = '/usr/bin/chromedriver'
brave_path = '/usr/bin/brave-browser'
option = webdriver.ChromeOptions()
option.binary_location = brave_path
browser = webdriver.Chrome(executable_path=driver_path, options=option)
browser.get("https://www.google.es")
<小时 />参考资料
您可以在以下位置找到一些相关的详细讨论:
- 弃用警告:在Windows 10系统上使用ChromeDriver和Chrome通过Selenium使用选项而不是chrome_options错误
- 如何在Windows上使用Selenium和Python启动Brave浏览器
- 弃用警告:使用选项而不是chrome_options错误使用带有 Python Selenium 和 Chromedriver 的 Brave 浏览器
这也适用于带有 Brave 浏览器的 Windows 10。 我下载了Chromedriver并将其放在Brave.exe的文件夹中。
from selenium import webdriver
driver_path = "C:\Users\5150s\AppData\Local\Programs\Python\Python38\chromedriver.exe"
brave_path = "C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe"
option = webdriver.ChromeOptions()
option.binary_location = brave_path
browser = webdriver.Chrome(executable_path=driver_path, options=option)
browser.get("https://www.google.es")
上面的解决方案给了我一些错误。此代码删除可执行文件路径和选项错误。 Chromedriver 位于 pycharm 文件夹中。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
driver_path = "C:/Users/johnm/PycharmProjects/chromedriver.exe"
brave_path = "C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe"
s=Service(driver_path)
option = webdriver.ChromeOptions()
option.binary_location = brave_path
browser = webdriver.Chrome(service=s, options=option)
browser.get("https://www.google.es")