Search code examples
pythonselenium-webdrivermicrosoft-edge

Connecting on already running browser with Python


I would like some help to see if there is a way around this problem. I'm testing to make a script that can connect to the browser that is launched by the user.

What I can do with success:

I CAN connect to a browser that is launched by this command:

"c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9244 --user-data-dir="C:\Users\user\OneDrive\Plocha\záložky\msedge1"

The command must have a specified user data directory which must be different from the basic user edge data. (different from this: C:\Users\user\AppData\Local\Microsoft\Edge\User Data)


What I can't manage to do:

The goal is to connect to the browser launched by the user. So I was trying to find the port of edge processes from this command: netstat -AB. I copy the IP address that was with the process name: msedge.exe and pasted it into the script. It thought for a minute and it came up with this result: https://i.sstatic.net/mGL4d.png


The script:

import selenium
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service

edge_options = Options()
edge_options.add_experimental_option("debuggerAddress","127.0.0.1:55718")

edge_options.use_chromium = True
s = Service('C:/Users/WDAGUtilityAccount/Downloads/edgedriver_win64/msedgedriver.exe')
driver = webdriver.Edge(service = s, options = edge_options)

print(driver.title)

Solution

  • You won't be able to bind to the Edge browser's devtools protocol without explicitly starting an Edge instance with your debugging arguments applied. At the time of writing, I cannot find a way to apply these settings to Edge by default.

    However, it could be worth worth adding your debugging arguments --remote-debugging-port=9244 --user-data-dir="C:\Users\user\OneDrive\Plocha\záložky\msedge1" to the Windows Shortcut in:

    C:\ProgramData\Microsoft\Windows\Start Menu\Programs

    Microsoft Edge (Right-Click) > Properties > Target (append your debugging arguments.) Every time you start edge through this shortcut, your debugger will be enabled.

    Please note, for what it's worth, this is called remote-debugging because it is designed to debug. Please consider the risks of exposing your day-to-day production browsing data on an unauthenticated network port.