How to interact using Selenium with already opened browser?

764 Views Asked by At

Till a few days back what was working perfectly:

Open browser with: "C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe" --remote-debugging-port=9222

then in python I check the response status code (should be 200) using GET request to http://localhost:9222.

then attach selenium:

options = Options()
options.binary_location = "C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe"
options.add_argument("disable-popup-blocking")
options.add_experimental_option("debuggerAddress",
        socket.gethostbyname("localhost:9222")
driver = webdriver.Chrome(ChromeDriverManager().install(), options = options)

However, this setup is not working anymore, as nothing can be accessed through http://localhost:9222 now with new updates.

Any idea how to achieve the same?

1

There are 1 best solutions below

0
AbiSaran On

Try this code, it's working:

Run the below command in command prompt:

"C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe" --remote-debugging-port=9222 --user-data-dir="C:\\Temp\\BraveData"

Brave browser will be launched, then use the below code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from webdriver_manager.core.utils import ChromeType

options = Options()
options.add_experimental_option("debuggerAddress", "localhost:9222")

driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()), options = options)