Get Selenium to work with Brave browser on Linux

1.2k views Asked by At

System:
OS: Ubuntu 20.04.2 LTS
Python: 3.8.5
selenium: 3.141.0
ChromeDriver: 90.0.4430.24
Brave: Version 1.24.86 Chromium: 90.0.4430.212 (Official Build) (64-bit)

I am trying to get Selenium to work with Brave. I am using the script from here.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = '/snap/bin/brave'
driver_path = '/usr/local/bin/chromedriver'
drvr = webdriver.Chrome(options=options, executable_path=driver_path)
drvr.get('https://stackoverflow.com')

I keep getting this error

selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist

And I have tried all the answers in stackflow I tried all different combinations of these arguments but nothing worked:(

options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_argument('--remote-debugging-port=9222')
options.add_argument('--headless')

Can anyone help me out?

1

There are 1 answers

0
ylitc On BEST ANSWER

I was able to get it working by changing the remote-debugging-port to a different port.

Here is my code that worked.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
#driver_path = '/usr/local/bin/operadriver'
driver_path = '/usr/local/bin/chromedriver'
brave_path = '/snap/bin/brave'

options.binary_location = brave_path
options.add_argument('--remote-debugging-port=9224') #NOT 9222

drvr = webdriver.Chrome(options=options, executable_path=driver_path)
drvr.get('https://stackoverflow.com')
drvr.quit

I did see a tip here about using an opera driver, which i found here, but it did not solve my error.

The issue was that after running the script once the process was not killed and was still running in the background so the port was taken and failed on all subsequent runs until I changed the port (or killed the process). The question is why is close() not working in brave when headless, it is working for chrome!