About remote debugging of Chrome #155
Replies: 3 comments 5 replies
-
@hanamichi77777 thanks for starting this discussion. Can you elaborate on this issue? What version of Chrome browser are you using? When you say "In the latest version of Chrome, access from Selenium is no longer possible.", are you talking about connecting to a previously open browser instance via I can perform the latter ok on my system (Chrome v136.0.7103.93). I did notice that running Do you know if this change is a Chrome/WebDriver bug? If so, do you have any references? Using MS Edge works as usual and does not seem to be affected (yet). Sub test_remoteDebugger()
'This test demonstrates how to connect to an existing Edge\Chrome browser instance and automate tasks.
'An example usage case - you need to manually login to a website first and then run some automation tasks.
'Below creates a shortcut on the user's Desktop to the browser's executable on port 9222 using the default profile.
'Then we open the browser before starting the WebDriver - in a typical use case all of above would be performed manually.
'Then we start WebDriver, setting the debugger address to port 9222 and perform automated tasks using SeleniumVBA.
'To manually create a shortcut on Desktop type this into a new shortcut location:
'"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222 --user-data-dir="<path to temp user profile directory>"
'"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir="<path to temp user profile directory>"
Dim driver As SeleniumVBA.WebDriver
Dim keys As SeleniumVBA.WebKeyboard
Dim caps As SeleniumVBA.WebCapabilities
Dim wshell As Object
Dim browser As String
Dim keySeq As String
Dim debugProfile As String
Set keys = SeleniumVBA.New_WebKeyboard
Set driver = SeleniumVBA.New_WebDriver
Set wshell = CreateObject("WScript.Shell")
browser = "chrome" '"chrome" or "msedge"
'--------------------------------------------------------------------------------------------
'In typical use case this section would be performed manually
'If you manually created the Desktop shortcut shown earlier, and opened your browser via the shortcut
'then this code block is redundant. It is here for testing only.
Select Case browser
Case "chrome"
'first must kill existing chrome processes
wshell.Run "taskkill /f /t /im chrome.exe", 0, True
'create a shortcut on the Desktop
With wshell.CreateShortcut(wshell.SpecialFolders("Desktop") & "\Chrome_test.lnk")
.targetPath = Environ("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
.WorkingDirectory = Environ("ProgramFiles") & "\Google\Chrome\Application"
'.Arguments = "--remote-debugging-port=9222 --profile-directory=""Default"""
'--------------------------------------------------------------------------------------------
'with above line this fails in Chrome 136 - replaced above with
'@hanamichi77777's code line and it works!
'see https://developer.chrome.com/blog/remote-debugging-port for more info...
'--------------------------------------------------------------------------------------------
debugProfile = Environ("USERPROFILE") & "\Documents\ChromeDebugProfile"
.Arguments = "--remote-debugging-port=9222 --user-data-dir=""" & debugProfile & """"
.Save
End With
'open the shortcut made above
wshell.Run wshell.SpecialFolders("Desktop") & "\" & "Chrome_test.lnk"
Case "msedge"
'first must kill existing edge processes
wshell.Run "taskkill /f /t /im msedge.exe", 0, True
'create a shortcut on the Desktop
With wshell.CreateShortcut(wshell.SpecialFolders("Desktop") & "\Edge_test.lnk")
.targetPath = Environ("programfiles(x86)") & "\Microsoft\Edge\Application\msedge.exe"
.WorkingDirectory = Environ("programfiles(x86)") & "\Microsoft\Edge\Application"
.Arguments = "--remote-debugging-port=9222 --profile-directory=""Default"""
.Save
End With
'open the shortcut made above
wshell.Run wshell.SpecialFolders("Desktop") & "\" & "Edge_test.lnk"
End Select
'--------------------------------------------------------------------------------------------
'now we start automating the existing browser instance
Select Case browser
Case "chrome": driver.StartChrome
Case "msedge": driver.StartEdge
End Select
Set caps = driver.CreateCapabilities(initializeFromSettingsFile:=False)
'set debugger address to same port as browser
caps.SetDebuggerAddress "localhost:9222"
driver.OpenBrowser caps
driver.NavigateTo "https://www.wikipedia.org/"
driver.Wait 1000
keySeq = "Leonardo da VinJci" & keys.LeftKey & keys.LeftKey & keys.LeftKey & keys.DeleteKey & keys.ReturnKey
driver.FindElement(By.ID, "searchInput").SendKeys keySeq
driver.Wait 1500
'this will close the pre-existing browser window (thanks to @hanamichi7777)
'driver.ExecuteCDP "Browser.close"
driver.CloseBrowser
driver.Shutdown
End Sub |
Beta Was this translation helpful? Give feedback.
-
Ok, awesome - thanks for the clarification and the link! |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the latest version of Chrome, access from Selenium is no longer possible. It will work if you set it as follows when starting.
wshell.Run "chrome.exe --remote-debugging-port=9222 --user-data-dir=C:\ChromeDebugProfile
(Additional)2025/6/2
The location and name of the folder are not fixed.
If you do not create it in advance, it will be created automatically.
Beta Was this translation helpful? Give feedback.
All reactions