Home > Net >  Use Selenium with Brave Browser pass service object written in python
Use Selenium with Brave Browser pass service object written in python

Time:11-16

#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 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

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.

CodePudding user response:

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/

CodePudding user response:

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:

  • Related