Automating existing browser session #74
Replies: 4 comments
-
On your Application.OnKey Excel focus issue - I know of no clean solution. It looks like you can create a windows hotkey to a VBScript file shortcut that resides on either your desktop or windows program startup directory but that idea is probably too messy to pursue. On the SwitchToWindow issue - wow, that's pretty cool - thanks for sharing! I see that there are even more http endpoints that can be invoked in debugger mode. You should consider adding a use-case example in the test modules, if you come up with a good one. FYI don't forget that there is the PageToJsonObject method that could be useful for parsing the resulting json: Dim json As Collection
Set json = .PageToJSONObject
For i = 1 To json.Count
If json(i)("title") = "Pippo.com: who am I" Then
windowHandle = json(i)("id")
Exit For
End If
Next i |
Beta Was this translation helpful? Give feedback.
-
Any new insights concerning attaching to an existing browser session? I wonder what other cdp commands can be sent to the browser? I reran (since your original post) a version of your script above with Chrome browser and the SwitchToWindow failed - it now works if I leave off the "CdWindow-" prefix for the window handle. Have you determined the same? Also, I tried attaching to Edge browser and it fails with a timeout on OpenBrowser. Do you know what I am doing wrong? Sub test_remoteDebugger()
'THIS WORKS FOR GCUSER99 USING CHROME, BUT NOT EDGE
Dim driver As SeleniumVBA.WebDriver
Dim keys As SeleniumVBA.WebKeyboard
Dim caps As SeleniumVBA.WebCapabilities
Dim wshell As Object
Dim keySeq As String
Set keys = SeleniumVBA.New_WebKeyboard
Set driver = SeleniumVBA.New_WebDriver
'must start the browser on the debugging port
Set wshell = CreateObject("WScript.Shell")
'wshell.Run "chrome.exe --remote-debugging-port=9222"
wshell.Run "msedge.exe --remote-debugging-port=9222"
'driver.StartChrome
driver.StartEdge
Set caps = driver.CreateCapabilities(initializeFromSettingsFile:=False)
'set debugger address to same port as browser
caps.SetDebuggerAddress "localhost:9222"
'Debug.Print caps.ToJson
driver.OpenBrowser caps '<-- FAILS HERE
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
driver.CloseBrowser
driver.Shutdown
End Sub Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
I just tried again my original version on my Edge 114.0.1823.41 (Official build) (64-bit), and I just had to remove "CDwindow-" to make it work. Note 1: I opened Edge via a shortcut icon with the argument "--remote-debugging-port=9222" Note 2: if Edge is already open without that argument, it's necessary to kill its process(es) before launching it with that argument. Simply closing the visible Edge window is not sufficient. Manually verifying that "localhost:9222/json" works proved to be a reliable predictor of it working with SeleniumVBA. Note 3: some tests showed me that only the IDs of type "page" work with SwitchToWindow. The other types, namely "iframe, "background_page" and "service_worker," don't work, presumably because they are not even tabs. |
Beta Was this translation helpful? Give feedback.
-
ahhh - thx that helps! Based on your answer above, the following works for me consistently... Sub test_remoteDebugger()
Dim driver As SeleniumVBA.WebDriver
Dim keys As SeleniumVBA.WebKeyboard
Dim caps As SeleniumVBA.WebCapabilities
Dim wshell As Object
Dim keySeq As String
Set keys = SeleniumVBA.New_WebKeyboard
Set driver = SeleniumVBA.New_WebDriver
'must start the browser on the debugging port
Set wshell = CreateObject("WScript.Shell")
'kill edge processes
wshell.Run "taskkill /f /t /im msedge.exe", 0, True
'open browser
wshell.Run "msedge.exe http://localhost:9222/json --remote-debugging-port=9222"
driver.StartEdge
Set caps = driver.CreateCapabilities(initializeFromSettingsFile:=False)
'set debugger address to same port as browser
caps.SetDebuggerAddress "localhost:9222"
'Debug.Print caps.ToJson
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
driver.CloseBrowser
driver.Shutdown
End Sub |
Beta Was this translation helpful? Give feedback.
-
I am doing my first experiments using SeleniumVBA and the new SetDebuggerAddress (thanks Mike!) to automate tasks on tabs that are already open in my main (manual) browser instance.
However, I encountered a couple of obstacles:
To solve the second one, the most fast and neutral way I discovered is to read and parse the page http://localhost:9222/json, which I found to contain the window handle, title and url of every open tab (and more...).
Here is my testing code:
Do you have any suggestion?
Beta Was this translation helpful? Give feedback.
All reactions