Use Selenium with Brave Browser pass service object written in python
Asked Answered
E

2

1

#TLDR I want to use brave browser with selenium written in python but can't find any current solutions that work.

This code works

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

but executable_path is deprecated:

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)

Found this on 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 driverSerice 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

When I run this code I get: selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

This code works as long as you allow Google Chrome onto your PC. I don't want Chrome on my PC.

The problem is that I can't figure out how to get selenium to use brave instead of Chrome.

As of this writing I am using the following:
Windows 11 Home
Selenium v4.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)

Can some one please explain how to make this work with the current (read nondeprecated) code on brave browser? Thanks for your time.

Esurient answered 15/11, 2021 at 6:36 Comment(0)
A
1

To initiate a browsing context you need to:

  • Use the binary_location attribute to point to the brave binary location.
  • Use the chromedriver executable to initiate the brave browser.

Code block:

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

Note: The DeprecationWarning: executable_path has been deprecated is a harmless warning message which doesn't affects your test execution and you can still ignore it.


References

You can find a couple of relevant detailed discussions in:

Adagietto answered 15/11, 2021 at 21:44 Comment(3)
there's a reason that the coders bothered to make the deprecation warning. I don't think its wise to use deprecated code. The proper way is to pass the service object. This is what I'm trying to do for Brave.Esurient
proper way is to pass the service object: That's what my code is based on.Adagietto
My apologies. My eyes went straight to the Note. I assumed that you had just repeated the same code without actually looking at the code.Esurient
N
0

you have to set your path to brave binary.

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

Go through this website:

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

Neoteny answered 15/11, 2021 at 6:48 Comment(1)
This script mentioned in the link is written in java. I can't and don't want to transcode this to python. I don't know java. please transcode to python.Esurient

© 2022 - 2024 — McMap. All rights reserved.