[Excel VBA] How to Wait for Microsoft Edge to Finish Loading and Get Page Content

目次

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

ItemProcedure
SeleniumBasicDownload the installer from the GitHub release page and install it normally.
EdgeDriverDownload msedgedriver.exe corresponding to your Edge version from the official Microsoft site. Place it in the C:\SeleniumBasic folder (default installation location).
ReferencesIn 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 ValueIE ConstantEdge (Selenium) StateDescription
0READYSTATE_UNINITIALIZEDBefore initialization
1READYSTATE_LOADING“loading”Loading
2READYSTATE_LOADED“interactive”DOM construction complete, sub-resources incomplete
3READYSTATE_INTERACTIVE“interactive”Same as above
4READYSTATE_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.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次