[Excel VBA] How to Extract Specific Elements from Microsoft Edge Using SeleniumBasic

目次

Introduction

Since Windows 10, automating Internet Explorer (IE) is no longer recommended, and scraping using Microsoft Edge has become the mainstream approach.

This article explains how to use SeleniumBasic and EdgeDriver to access a specific class of elements after a page loads and write the data to an Excel sheet. We will use the site https://morinokabu.com/ as an example.

Prerequisites

ItemRequirement
OSWindows 10 / 11
ExcelMicrosoft 365 or Excel 2016+
ToolSeleniumBasic (VBA Wrapper)
Drivermsedgedriver.exe (Must match your Edge version)

Setup Steps:

  1. Install SeleniumBasic (Default location: C:\SeleniumBasic).
  2. Download msedgedriver.exe that matches your Edge version and place it in the installation folder.
  3. In the VBA Editor, go to Tools -> References and enable “Selenium Type Library”.

Sample Code

Option Explicit     ' Reference required: Selenium Type Library

Sub ScrapeMorinokabuData()

    Dim siteURL    As String
    Dim driver     As New Selenium.EdgeDriver
    Dim itemList   As Selenium.WebElements
    Dim element    As Selenium.WebElement
    Dim rowIndex   As Long
    
    siteURL = "https://morinokabu.com/"
    
    ' Start Edge
    driver.Start
    driver.Get siteURL
    
    ' Wait for page load completion
    Do While driver.ExecuteScript("return document.readyState") <> "complete"
        DoEvents
    Loop
    
    ' Get elements with class name "data"
    Set itemList = driver.FindElementsByCss(".data")
    
    rowIndex = 1
    For Each element In itemList
        ' Column A: First child element text
        Cells(rowIndex, 1).Value = element.FindElementsByCss("*")(0).Text
        
        ' Column B: HTML of the 3rd <p> element
        Cells(rowIndex, 2).Value = _
            element.FindElementsByTag("p")(2).Attribute("outerHTML")
        
        ' Column C: href of the first <a> element
        Cells(rowIndex, 3).Value = _
            element.FindElementsByTag("a")(0).Attribute("href")
        
        rowIndex = rowIndex + 1
    Next element
    
    driver.Quit

End Sub

Code Explanation

  • driver.ExecuteScript(“return document.readyState”)Checks the loading status using JavaScript. Loops until the status becomes “complete”.
  • FindElementsByCss(“.data”)Retrieves a group of elements that have the class data using a CSS selector.
  • FindElementsByCss(“*”)(0)References the first child element of the retrieved element.
  • Attribute(“outerHTML”)Retrieves the HTML string including the element itself.
  • Attribute(“href”)Retrieves the link destination of an <a> element.

Main DOM Access Methods

MethodDescription
FindElementById("ID_Name")Gets an element with a unique ID.
FindElementsByName("Name_Attr")Gets an array of elements with a specific name attribute.
FindElementsByClass("Class_Name")Gets an array of elements with a specific class.
FindElementsByTag("Tag_Name")Gets an array of elements with a specific tag name.

After retrieving an element, you can extract information by combining properties like InnerText, Attribute("outerHTML"), and Attribute("src").

Advanced Ideas

  • Variable Wait TimeUse driver.Wait 10000, “css=.data” to wait up to 10 seconds, proceeding as soon as the element appears.
  • Pages Requiring ScrollUse driver.ExecuteScript “window.scrollTo(0, document.body.scrollHeight);” to load the bottom of the page before searching for elements.
  • Crawling Multiple PagesStore URLs in an array and call driver.Get within a loop to process a large number of pages sequentially.

Summary

By using SeleniumBasic and EdgeDriver, you can directly access the DOM of Microsoft Edge via Excel VBA to extract any element you need.

When migrating old IE automation code, combining FindElement methods with JavaScript wait logic ensures stable operation. This is highly effective for automating internal reports and data collection.

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

この記事を書いた人

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

目次