Overview
The InternetExplorer.Application object used for Internet Explorer is being phased out. For Windows 10 and later, automating Microsoft Edge is recommended.
This article explains the steps to launch Edge from Excel VBA using SeleniumBasic and EdgeDriver, wait for the page to finish loading, and retrieve the text of a specific element.
As an example, we will retrieve the text from the element id="Article" on the site https://morinokabu.com/.
Prerequisites
| Item | Procedure |
| SeleniumBasic | Download the installer from the GitHub release page and install it normally. |
| EdgeDriver | Download msedgedriver.exe corresponding to your Edge version from the official Microsoft site. Place it in the C:\SeleniumBasic folder (default installation location). |
| References | In the VBA Editor, go to Tools -> References and check “Selenium Type Library”. |
Sample Code
Option Explicit
' Reference: Selenium Type Library is required
Sub FetchArticleText()
Dim pageURL As String ' Target URL
Dim driver As New Selenium.EdgeDriver
Dim pageText As String
pageURL = "https://morinokabu.com/"
' Launch Edge and open the page
driver.Start
driver.Get pageURL
' Wait for loading to complete (Loop until document.readyState becomes "complete")
Do While driver.ExecuteScript("return document.readyState") <> "complete"
DoEvents
Loop
' Get text of id="Article"
pageText = driver.FindElementById("Article").Text
' Check the result in a message box
MsgBox pageText, vbInformation, "Retrieved Text"
driver.Quit
End Sub
Key Points Explanation
- driver.ExecuteScript(“return document.readyState”)Executes JavaScript to get the page loading status. It transitions in the order of “loading” -> “interactive” -> “complete”.
- DoEventsAllows VBA screen updates and interruptions during the wait loop to prevent the application from freezing.
- FindElementById(“Article”).TextRetrieves the text of the element with the specified ID. You can also search using classes or XPath.
Correspondence Table: ReadyState vs document.readyState
| IE Value | IE Constant | Edge (Selenium) State | Description |
| 0 | READYSTATE_UNINITIALIZED | – | Before initialization |
| 1 | READYSTATE_LOADING | “loading” | Loading |
| 2 | READYSTATE_LOADED | “interactive” | DOM construction complete, sub-resources incomplete |
| 3 | READYSTATE_INTERACTIVE | “interactive” | Same as above |
| 4 | READYSTATE_COMPLETE | “complete” | All resources loaded |
Application Ideas
- Wait until an element is renderedUse driver.Wait 10000, “id=Article” to wait up to 10 seconds.
- Extract multiple elementsUse driver.FindElementsByCss(“div.article-item”) to get a collection.
- Pages requiring scrollingScroll to the bottom using driver.ExecuteScript “window.scrollTo(0, document.body.scrollHeight);”.
Summary
By combining SeleniumBasic and EdgeDriver, you can strictly detect Microsoft Edge page load completion and manipulate DOM elements freely using Excel VBA.
When migrating legacy IE automation code to Edge, please customize the code based on the steps and examples in this article.
