12

I switched from Google's Chrome to Brave web browser and am having a hard time getting it to work with Brave like it did with Chrome. Brave is based on chromium so I guessed it should not be that hard. I made sure that my Brave and Chromedriver are on the same version like this,

~/some/path $ chromedriver --version ChromeDriver 76.0.3809.126 (d80a294506b4c9d18015e755cee48f953ddc3f2f-refs/branch-heads/3809@{#1024}) 

My chromedriver is also in /user/bin,

~/path $ cd /usr/bin/ /usr/bin $ ls | grep chromedriver chromedriver 

And to check the Brave version, I get: Version 0.68.132 Chromium: 76.0.3809.132 (Official Build) (64-bit)

Then I run this code,

from selenium import webdriver driver = webdriver.Chrome(executable_path='/usr/bin/brave-browser') driver.get("http://www.python.org") driver.close() 

This opens a Brave window but then instead of getting the page the driver is pointed to, an exception is thrown,

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 

6 Answers 6

35

I finally managed to make it work:

Try this python script (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") 

cheers.

Sign up to request clarification or add additional context in comments.

Comments

8

The executable_path key is used to pass the absolute path of the WebDriver binary i.e. the chromedriver executable.

To initiate a Brave browser session additionally you have to pass the absolute location of the brave-browser binary through the binary_location argument of an instance of ChromeOptions.

So the effective code block will be:

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

References

You can find a couple of relevant detailed discussions in:

Comments

2

This also works in windows 10 with Brave browser. I downloaded Chromedriver and put it in the folder with 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") 

1 Comment

Make sure to download the driver that matches your Brave browser. Go to "About Brave" and look for the Chromium driver version, e.g. Chromium: 91.0.4472.164, then select that version from the Chromium download site: chromedriver.chromium.org/downloads.
1

The full-monty:

  • Brave
  • Python
  • Selenium (async)
  • ChromeDriver
  • ChromeDriverManager

set similar options as any chromium-based browser

def get_chrome_options( decorated:bool=False, fat :bool=False, headless :bool=False, sandbox :bool=True, )->ChromeOptions: options:ChromeOptions = ChromeOptions() options.browser = "brave-browser-stable" # TODO if (not decorated): options.add_argument("--hide-scrollbars") if headless: options.add_argument("--headless") options.add_argument("-headless") options.headless = headless if sandbox: options.add_argument("--disable-extensions") options.add_argument("--sandbox") else: logger.debug('no sandbox') options.add_argument('--user-data-dir=./somewhere') options.add_argument('--profile-directory=somewhere') #assert (not sandbox) if (not fat): options.add_argument("--disable-dev-shm-usage") options.add_argument("--single-process") options.add_argument("--disable-gpu") options.add_argument("--ignore-certficate-errors") options.add_argument("download.default_directory=./somewhere_else") preferences = { "download.default_directory":"./somewhere_else", "download.prompt_for_download":False, "download.directory_upgrade":True, "profile.default_content_setting_values.automatic_downloads": 2, } options.add_experimental_option("prefs", preferences) return options 

load the brave service without hard-coded paths

def get_brave_service()->ChromeService: try: executable_path = ChromeDriverManager(chrome_type=ChromeType.BRAVE).install() except AttributeError as error: logger.exception(error) executable_path = which('chromedriver') return ChromeService(executable_path) 

they're all pretty much the same from here

def get_chrome_driver(options:ChromeOptions, provider:str)->WebDriver: service:Optional[ChromeService] = None if (provider == "chrome"): service = get_chrome_service() if (provider == "chromium"): service = get_chromium_service() if (provider == "brave"): service = get_brave_service() if (service is None): raise ValueError(provider) return Chrome( options=options, service=service, ) 

so I'm using selenium-async, actually

def get_pool_helper(driver:WebDriver, options:Options)->Pool: driver.set_window_size(1600,800) pool :Pool = Pool(max_size=1) # we've gotta prevent that library from trying to allocate any Firefox's without the service setting pool.resources[options] = [driver,] # populate its internal mapping and make sure it doesn't mess with my settings return pool def get_chrome_pool(provider:str)->Tuple[Pool,ChromeOptions]: options:ChromeOptions = get_chrome_options(provider) if (provider == "brave"): options.browser = "chromium" options.binary_location = which("brave-browser-stable") driver :WebDriver = get_chrome_driver(options, provider) pool :Pool = get_pool_helper(driver, options) return (pool, options,) def get_pool(provider:str)->Tuple[Pool,Options]: if (provider == "firefox"): return get_firefox_pool() if (provider in ["chrome", "chromium", "brave",]): return get_chrome_pool(provider) if (provider == "ie"): return get_ie_pool() if (provider == "edge"): return get_edge_pool() if (provider == "opera"): return get_opera_pool() raise ValueError(provider) 

Comments

0

The solutions above gave me some errors. This code removes the executable path and options errors. Chromedriver is in the pycharm folder.

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

Comments

0

As of Selenium 4.15.2, apparently there is not anymore a need to create a Service. At least, on my Ubuntu installation (23), I could get Brave opened just using the following code.

 options = ChromeOptions() options.add_argument("start-maximized") options.binary_location = "/opt/brave.com/brave/brave" browser = Chrome(options=options) 

Still I can't figure out how to attach to the currently running session rather than opening a brand new testing session window.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.