9

我从 Google 的 Chrome 切换到了Brave 网络浏览器,并且很难让它像在 Chrome 上那样与 Brave 一起工作。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
4

4 回答 4

24

我终于设法使它工作:

试试这个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")

干杯。

于 2019-10-29T19:17:23.763 回答
3

executable_path密钥用于传递WebDriver二进制文件的绝对路径,即chromedriver可执行文件。

要启动Brave浏览binary_location器会话,您还必须通过ChromeOptions.

所以有效的代码块将是:

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

参考

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

于 2020-11-26T23:27:27.097 回答
1

这也适用于带有 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")
于 2020-06-09T01:05:17.983 回答
0

上面的解决方案给了我一些错误。此代码删除可执行路径和选项错误。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")
于 2021-12-25T17:43:53.623 回答