Automating Edge Browser using VBA without downloading Selenium
Asked Answered
G

4

7

I had the automation tools which are written for IE. Now, I want to re-write those same tools this time to use Microsoft Edge as the default browser. I could not find the alternative approach other than downloading the WebDriver which comes as part of the Selenium Package.

How can I automate the Edge browser without downloading/installing other software or web drivers?

Gleeson answered 7/1, 2022 at 9:41 Comment(0)
T
0

Which version of Selenium are you using? If you're using Selenium 3 or Selenium 4, the available languages are Java, Python, C#, Ruby, and JavaScript. You can refer to this doc for more information.

If you want to use Selenium to automate Edge in VBA, you can only use SeleniumBasic. You can refer to this thread for the detailed steps of automating Edge browser with SeleniumBasic.

Tench answered 10/1, 2022 at 6:17 Comment(1)
I did not use any Selenium version so far. My Intent was to write the code in VBA without installing any Selenium. Now, I clearly understood that I can not write the code to Automate Edge Browser without the SeleniumBasic Package which has the msedgedriver.exe file in the package where every time we need to download the edgedriver which is in compatible with the edge browser's release & version.Gleeson
A
14

There are now many ways to achieve this.

Method 1

As of 25th April 2022, you can now directly automate Edge IE Mode with VBA without any additional third party-software. The below guidance has been well tested by me and my colleagues after obtaining it from exchanging with our partnered Microsoft Support team.

Win Upgrade + Registry Fix

  1. Your Windows version needs to be at least 20H2. You can check your Windows version with this guide here.

  2. Your Windows needs to have the latest cumulative KB update past March 2022. You can check your Windows update history with this guide here.

  3. Finally install the below registry keys on your Windows and restart:

    Windows Registry Editor Version 5.00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\Main] "NotifyDisableIEOptions"=dword:00000002

    [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\Main\EnterpriseMode] "EnableGlobalWindowListInIEMode"=dword:00000001

Per the MS Support team, the above method should work until 2029. Official documentation on this might be coming soon I believe.

After the above steps, VBA shall be able to interact with Edge IE Mode as if it is an Internet Explorer window. Your current code that automates the InternetExplorer.Application object will work with Edge IE mode as well.

Method 2

You can also employ the following method written by ChrisK23 of CodeProject that makes use of Chrome Devtools Protocol to interact with Chromium-based browsers. The advantage of this method is that it allows VBA to interact directly with Edge without IE mode and also with Chrome.

Automate Chrome / Edge using VBA via CDP - Code Project

The article above also includes an example file which you can download and explore the method. However, do note that the example file is missing Microsoft Scripting Runtime reference which you need to include this manually afterwards to make it working.

With this method, you can now automate even Chrome without additional software installed.

I have created a dedicated Git with a demo file to this method here:

https://github.com/longvh211/Chromium-Automation-with-CDP-for-VBA

Method 3

Another method involves using winAPI to retrieve the HTML document object from Internet Explorer Server class of the running Edge IE Mode window.

Sample Code 1 Sample Code 2

Note: the above codes are for Office 32-bit. For Office 64-bit you will need to convert them (refer to this MSDN link for more details)

The pros of this method is that it is quite clean and interact well with Edge IE Mode without any additional setup or installation.

The cons is that it only works with Edge IE mode. Hence web applications that run only on Edge and not IE mode will not be automatable using this method.

You can refer to this dedicated Git in which I have prepared a demo file here:

https://github.com/longvh211/Edge-IE-Mode-Automation-with-IES-for-VBA

Antimatter answered 25/4, 2022 at 4:27 Comment(12)
Would you then write the same code as for IE? E.g. would you be instantiating an instance of Internet.Explorer via ieframe.dll (MS Internet Controls)?Volauvent
@Volauvent Yes you can keep the same VBA code that previously automates the InternetExplorer object. It will work the same for Edge IE mode.Antimatter
Maybe include the necessity to change the Internet Explorer compatibility settings in Edge (see How to use internet explorer mode in edge)Reg
Is there any information from Microsoft how they will handle this in the future without Internet Explorer? I can't install anything on my corporate computer and excel vba is the only way to automate some work and without Microsoft Internet Controls it is getting difficult.Ryter
@Ryter you will have to start converting your VBA scripts to work with Edge directly in that case. Give a try with the CDP method I mentioned above. It is quite powerful.Antimatter
@Antimatter First of all this is a great answer and thank you. I tried method 2. I tried to read your code and I understand a great deal of it, but I have a question. Can I somehow get the HTML document of the clsBrowser class?Ruck
I'm trying to get Method 1 to work, but my VBA calls to create an objectComestible
Set ie = CreateObject("InternetExplorer.Application")Comestible
Still opens Internet Explorer, not Edge in IE mode. Have any of these instructions changed?Comestible
@dimitris Yes using the appropriate Javascript method, you should be able to use the JsEval function to retrieve the entire HTML document.Antimatter
@Gordon Prince no it has not been changed. This is expected behavior as IE is still there in your computer by default so that Edge can make use of it for its IE mode.Antimatter
Your code example for method 1 requests the user to open the web page first, then switch to IE mode, then your VBA code will interact with the document. Is there a way to launch a web site using Edge in IE mode? Then interact with the document inside the browser object? My application needs to open the browser, navigate, capture information, then close the browser. Thanks.Comestible
X
4

You can consider to use Win API to achieve Edge automation. It doesn't require any installation and regular update of Edge driver. Please see my experiences to use Win API on Edge browser webpage automation :

  1. Place the following codes in a new blank module. I name this module as “MsEdge” usually. The codes in this module are no need to modify for usage. You can directly use the codes even if you don’t know much about Win API.

    Public lngProcessID_Close As Long
    
    'Part 1 --- Locate IES
    
    Private strHwndIES As String
    
    Private lngHwndIndex As Long
    
    Private Declare Function EnumWindows Lib "user32" ( _
        ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    
    Private Declare Function EnumChildWindows Lib "user32" ( _
        ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
        ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
    'Part 2 --- Get HTMLDocument from IES
    
    Private Const SMTO_ABORTIFHUNG = &H2
    
    Private Const GUID_IHTMLDocument2 = "{332C4425-26CB-11D0-B483-00C04FD90119}"
    
    Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" ( _
        ByVal lpString As String) As Long
    
    Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" ( _
        ByVal hWnd As Long, _
        ByVal msg As Long, _
        ByVal wParam As Long, _
        lParam As Any, _
        ByVal fuFlags As Long, _
        ByVal uTimeout As Long, _
        lpdwResult As Long) As Long
    
    Private Declare Function IIDFromString Lib "ole32" ( _
        lpsz As Any, lpiid As Any) As Long
    
    Private Declare Function ObjectFromLresult Lib "oleacc" ( _
        ByVal lResult As Long, _
        riid As Any, _
        ByVal wParam As Long, _
        ppvObject As Any) As Long
    
    'Part 3 --- Check Process Name
    
    Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
        ByVal hWnd As Long, lpdwProcessId As Long) As Long
    
    
    Public Function findEdgeDOM(Title As String, URL As String) As Object
    
        'Find criteria-hitting Edge page in IE mode
    
        Dim hwndIES As Long
    
        Do
    
            hwndIES = enumHwndIES
    
            If hwndIES Then
    
                Set findEdgeDOM = getHTMLDocumentFromIES(hwndIES)
    
                If Not findEdgeDOM Is Nothing Then
    
                    If InStr(findEdgeDOM.Title, Title) * InStr(findEdgeDOM.URL, URL) Then
    
                        Do
    
                            hwndIES = enumHwndIES
    
                        Loop While hwndIES
    
                        Exit Function
    
                    Else
    
                        Set findEdgeDOM = Nothing
    
                    End If
    
                End If
    
            End If
    
        Loop While hwndIES
    
    End Function
    
    Public Function enumHwndIES() As Long
    
        'Get all hwnds of IES
    
        If Len(strHwndIES) = 0 Then
    
            EnumWindows AddressOf EnumWindowsProc, 0
    
            lngHwndIndex = 0
    
        End If
    
        'Exit function when overflow
    
        If lngHwndIndex + 1 > (Len(strHwndIES) - Len(Replace(strHwndIES, ",", ""))) Then
    
            enumHwndIES = 0
    
            strHwndIES = ""
    
            Exit Function
    
        End If
    
        'Return IES hwnd one by one
    
        enumHwndIES = CLng(Split(Left(strHwndIES, Len(strHwndIES) - 1), ",")(lngHwndIndex))
    
        lngHwndIndex = lngHwndIndex + 1
    
    End Function
    
    Private Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
    
        Dim lngProcessID As Long
    
        GetWindowThreadProcessId hWnd, lngProcessID
    
        EnumChildWindows hWnd, AddressOf EnumChildProc, lngProcessID
    
        EnumWindowsProc = True
    
    End Function
    
    Public Function EnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
    
        Dim strTargetClass As String, strClassName As String
    
        strTargetClass = "Internet Explorer_Server"
    
        strClassName = getClass(hWnd)
    
        If strClassName = strTargetClass Then
    
            If GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process WHERE ProcessId='" & lParam & "' AND Name='msedge.exe'").Count Then
    
                strHwndIES = strHwndIES & hWnd & ","
    
                lngProcessID_Close = lParam
    
                EnumChildProc = False
    
                Exit Function
    
            End If
    
        End If
    
        EnumChildProc = True
    
    End Function
    
    Private Function getClass(hWnd As Long) As String
    
        Dim strClassName As String
    
        Dim lngRetLen As Long
    
    
        strClassName = Space(255)
    
        lngRetLen = GetClassName(hWnd, strClassName, Len(strClassName))
    
        getClass = Left(strClassName, lngRetLen)
    
    End Function
    
    Public Function getHTMLDocumentFromIES(ByVal hWnd As Long) As Object
    
        Dim iid(0 To 3) As Long
    
        Dim lMsg As Long, lRes As Long
    
        lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT")
    
        SendMessageTimeout hWnd, lMsg, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes
    
        If lRes Then
    
            IIDFromString StrPtr(GUID_IHTMLDocument2), iid(0)
    
            ObjectFromLresult lRes, iid(0), 0, getHTMLDocumentFromIES
    
        End If
    
    End Function
    
    Public Sub closeEdge(Title As String, URL As String)
    
        'Close a Edge browser (the last one in EnumWindows order) with criteria-hitting webpage
    
        lngProcessID_Close = 0
    
        Dim findEdgeDOM As Object
    
        Dim hwndIES As Long
    
        Do
    
            hwndIES = enumHwndIES
    
            If hwndIES Then
    
                Set findEdgeDOM = getHTMLDocumentFromIES(hwndIES)
    
                If InStr(findEdgeDOM.Title, Title) * InStr(findEdgeDOM.URL, URL) Then
    
                    Shell "TaskKill /pid " & lngProcessID_Close
    
                    Do
    
                        hwndIES = enumHwndIES
    
                    Loop While hwndIES
    
                    Exit Sub
    
                End If
    
            End If
    
        Loop While hwndIES
    
    End Sub
    
  2. Apply the functions in “MsEdge” module. There are a few application examples for you. Suggest to place and test below codes at another module:

    Sub findEdgeDOM_DemoProc()
    
        'Demo Proc : Use findEdgeDOM Function to get DOM of specific Edge webpage by Title AND URL
    
        'Dim docHTML As MSHTML.HTMLDocument     '--- Early Binding
    
        Dim docHTML As Object                   '--- Late Binding
    
        Set docHTML = findEdgeDOM("Enter Part of Webpage Title Here", "Enter Part of Webpage URL Here")
        ‘You can fill just one argument with either part of webpage title or URL as keyword to search for the target browser and leave another one blank (“”). If you provide both title and URL, the funcitons return DOM of the only browser that meets both criteria.
    
        If Not docHTML Is Nothing Then Debug.Print docHTML.Title, docHTML.URL
    
    End Sub
    
    Sub goEdge()
    
        'Go through every Edge webpage (opened in IE mode) and print out hwndIES, webpage Title & webpage URL
    
        Dim hwndIES As Long
    
        'Dim docHTML As MSHTML.HTMLDocument     '--- Early Binding
    
        Dim docHTML As Object                   '--- Late Binding
    
        Do
    
            hwndIES = enumHwndIES
    
            If hwndIES Then
    
                Set docHTML = getHTMLDocumentFromIES(hwndIES)
    
                Debug.Print hwndIES, docHTML.Title, docHTML.URL
    
            Else
    
                Debug.Print "Procedure End"
    
            End If
    
        Loop While hwndIES
    
    End Sub
    
    Sub openEdgeByURL_DemoProc()
    
        'Open Edge browser to specific URL
    
        openEdgeByURL "Input Webpage URL Here"
    
    End Sub
    
    Public Sub openEdgeByURL(URL As String)
    
        'Please change the path to your msedge.exe location in your PC
    
        Shell "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe -url " & URL, vbNormalFocus
    
    End Sub
    
    Sub closeEdge_DemoProc()
    
        'Close Edge browser
    
        closeEdge "Enter Part of Webpage Title Here", "Enter Part of Webpage URL Here"
    
    End Sub
    
Ximenes answered 16/5, 2022 at 23:56 Comment(1)
Works great, thanks, Kelvin. I've implemented this method and it works great. One hiccup I'm experiencing is how to interact with "Do you want to open or save xxx?" In I.E., this was achievable via FindWindowEx library, however using this method I'm unable to interact with this pop-up in Edge.Women
C
1

you can use webdrivermanager package. for that below is the depedency:

For Maven

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>4.4.3</version>
</dependency>

For Gradle:

implementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '4.4.3'

You can use below code in your class where u r initiating your browser:

WebDriverManager.edgedriver().setup()
Civil answered 7/1, 2022 at 14:12 Comment(3)
Hi Sonali, I am pretty much very new to this topic. I had studied a lot after your response for setting up the edge driver. But could you please help me out bit more on how to implement this driver management. Currently, I had installed SeleniumBasic Package and installed the msedgedriver.exe which is in compatible with the edge browser in my Machine. My Ask: How can I use the above code which will dynamically check for the edgedriver and edge browser's release & version without manually downloading it. I am using VBA Editor for Excel. Please help...Gleeson
Please go through wendriver manager code. The code is written to detect dynamically the browser version and provide driver based on the browser name provided.Civil
Is this for Java rather than VBA automation?Volauvent
T
0

Which version of Selenium are you using? If you're using Selenium 3 or Selenium 4, the available languages are Java, Python, C#, Ruby, and JavaScript. You can refer to this doc for more information.

If you want to use Selenium to automate Edge in VBA, you can only use SeleniumBasic. You can refer to this thread for the detailed steps of automating Edge browser with SeleniumBasic.

Tench answered 10/1, 2022 at 6:17 Comment(1)
I did not use any Selenium version so far. My Intent was to write the code in VBA without installing any Selenium. Now, I clearly understood that I can not write the code to Automate Edge Browser without the SeleniumBasic Package which has the msedgedriver.exe file in the package where every time we need to download the edgedriver which is in compatible with the edge browser's release & version.Gleeson

© 2022 - 2024 — McMap. All rights reserved.