Search code examples
pythonmacosselenium-webdrivermacos-catalinabrave-browser

How to use Brave web browser with Selenium on Catalina?


https://www.codegrepper.com/code-examples/python/python+selenium+brave+browser

I see this example to use brave browser on windows. Is it supposed to work on Catalina as well by just replacing driver_path and brave_path?

Also, Chromedriver is only for Chrome. How to determine which version of chromedriver should be used for brave browser?

https://chromedriver.chromium.org

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

Solution

  • Prerequisites:
    Your chromedriver version should match your Brave Browser web driver version.

    To make sure it does:

    • Check ChromeDriver version with brew info chromedriver. Along the lines of the output it should read chromedriver: 89.0.4389.23 (latest version as of writing this post)
    • Open Brave Browser, in menu bar click Brave -> About Brave. Along the lines it should read Version 1.22.71 Chromium: 89.0.4389.114 (Official Build) (x86_64) (again, latest as of writing this post)
    • These two should match, however, i am not entirely sure to which degree, since, as you can see here, last entries (.23 and .114) don't match, yet this works perfectly fine on my machine (macOS Big Sur 11.2.3) I don't think macOS version should really matter, but i still mentioned it for the sake of completeness.

    Finally run the following code (replace paths with ones on your machine if they are different):

    from selenium import webdriver  
    driverPath = '/usr/local/Caskroom/chromedriver/89.0.4389.23/chromedriver'
    binaryPath = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
    options = webdriver.ChromeOptions()
    options.binary_location = binaryPath
    browser = webdriver.Chrome(executable_path=driverPath, options=options)
    browser.get("https://www.google.com")
    

    If you have never used chromedriver before, after runnning the code you should see a macOS prompt saying that chromedriver is from unknown developer or was downloaded from the internet, smth like that. Close that prompt (It's important that you do this before moving on). Then go to System Preferences -> Security & Privacy -> press the lock icon and unlock it and then approve chromedriver to run on your machine. Run the above code again, a new macOS prompt will appear saying smth about unknown developer again, this time you can just click Open. Brave Browser window should pop up at this point. At least it did on my machine.
    P.S. I apologise for possibly going into too much detail, but sometimes i get really frustrated with answers which skip parts which are considered to be obvious