Automating Microsoft Edge Form Input and Submission with Excel VBA and SeleniumBasic

目次

Overview

This article explains how to control Microsoft Edge from Excel VBA to input keywords into a web search form and submit it. In this example, we will use the site https://morinokabu.com/ to demonstrate the process of retrieving form elements, setting values, and submitting data.

Prerequisites

ItemDetails
OSWindows 10 / 11
ExcelMicrosoft 365 or Excel 2016+
Additional ToolSeleniumBasic (VBA Wrapper)
DriverEdgeDriver (msedgedriver.exe)
VBA Reference“Selenium Type Library”

Setup Steps

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

Sample Code

Option Explicit   ' Reference: Selenium Type Library is required

Sub SubmitSearchForm()
    
    Dim driver     As New Selenium.EdgeDriver
    Dim targetURL  As String
    Dim keywordBox As Selenium.WebElement
    Dim submitBtn  As Selenium.WebElement
    
    targetURL = "https://morinokabu.com/"
    
    ' Launch Edge and load the page
    driver.Start
    driver.Get targetURL
    
    ' Wait for DOM to load completely
    Do While driver.ExecuteScript("return document.readyState") <> "complete"
        DoEvents
    Loop
    
    ' Get form elements
    Set keywordBox = driver.FindElementById("searchFormKeyword")
    Set submitBtn  = driver.FindElementById("searchFormSubmit")
    
    ' Input keyword
    keywordBox.Clear
    keywordBox.SendKeys "Excel VBA"
    
    ' Submit
    submitBtn.Click
    
    ' Add next page processing if necessary
    ' Example: Wait for result page to load
    Do While driver.ExecuteScript("return document.readyState") <> "complete"
        DoEvents
    Loop
    
    ' Quit
    driver.Quit
    
End Sub

Code Explanation

ProcessDescription
FindElementById(“searchFormKeyword”)Retrieves the input box and uses SendKeys to enter the keyword.
FindElementById(“searchFormSubmit”)Retrieves the submit button and uses Click to submit the form.
driver.ExecuteScript(…)Checks the loading state (“complete”) to wait for the page to load fully.

Main Methods for Retrieving Elements

MethodUsage & Features
FindElementById(“id”)The fastest and most reliable method when the ID is unique.
FindElementsByName(“name”)Gets an array of multiple elements sharing a common name attribute.
FindElementsByClass(“class”)Gets a list of elements belonging to the same class.
FindElementsByCss(“selector”)Retrieves elements flexibly using CSS selectors.
FindElementsByTag(“tagname”)Gets elements by tag name (e.g., input, a) in bulk.

Advanced Applications

  • Continuous Posting to Multiple Pages: Manage URLs in an array and loop through them using For Each.
  • Dynamic Input Values: Pass cell values or UserForm inputs to SendKeys.
  • Result Analysis: Use XPath or CSS selectors to retrieve result elements after submission and write them to an Excel sheet.
  • Error Handling: Combine On Error statements with driver.IsElementPresent() to check if elements exist.

Summary

By using SeleniumBasic and EdgeDriver, you can automate Microsoft Edge form operations directly from Excel VBA.

You can efficiently handle tasks such as searching or posting data by retrieving input boxes and buttons, then using SendKeys and Click. This approach is highly effective for automating data entry into business web systems or performing regular search tasks.

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

この記事を書いた人

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

目次