Skip to Content

Web Automation using Excel VBA Selenium

To automate web development tasks using Selenium and Excel VBA, you need the Selenium Basic library installed in your system. Selenium Basic is a wrapper for Selenium that works with VBA. 

Example of VBA code to automate a web task using Selenium. This code opens a website, fills out a form, and retrieves some data.

Automating a Web using VBA Selenium

  1. Setup Requirements:
    • Install Selenium Basic.
    • Download the appropriate browser driver (e.g., ChromeDriver for Chrome, GeckoDriver for Firefox) and place it in the Selenium Basic folder.
  2. VBA Code:
Sub SeleniumAutomation()

    ' Declare Selenium objects
    Dim driver As New WebDriver
    Dim excelRow As Integer
    Dim url As String
    Dim ws As Worksheet
    
    ' Define the Excel worksheet to work with
    Set ws = ThisWorkbook.Sheets("Sheet1")
    
    ' Define the website URL
    url = "https://example.com/form"
    
    ' Start a new browser session
    driver.Start "chrome"
    driver.Get url
    
    ' Wait for the page to load
    driver.Wait 5000
    
    ' Example: Fill out a form using data from Excel
    excelRow = 2 ' Start from the second row (assuming headers in row 1)
    Do While ws.Cells(excelRow, 1).Value <> ""
        ' Fill out form fields
        driver.FindElementById("firstName").SendKeys ws.Cells(excelRow, 1).Value
        driver.FindElementById("lastName").SendKeys ws.Cells(excelRow, 2).Value
        driver.FindElementById("email").SendKeys ws.Cells(excelRow, 3).Value
        
        ' Submit the form
        driver.FindElementById("submit").Click
        
        ' Wait for the result page to load
        driver.Wait 2000
        
        ' Retrieve data from the result page (example: confirmation message)
        ws.Cells(excelRow, 4).Value = driver.FindElementById("confirmationMessage").Text
        
        ' Go back to the form page for the next entry
        driver.Back
        driver.Wait 2000
        
        ' Move to the next row in Excel
        excelRow = excelRow + 1
    Loop
    
    ' Quit the browser
    driver.Quit
    
    MsgBox "Automation completed successfully!"
    
End Sub

Explanation:

  1. Initial Setup:
    • Start by initializing the Selenium WebDriver and defining the target website.
  2. Automation:
    • Use driver.FindElementById or other element-finding methods (FindElementByClass, FindElementByXPath, etc.) to locate HTML elements and perform actions like SendKeys (for typing) and Click (for clicking buttons).
  3. Excel Integration:
    • Use VBA to read from and write to Excel cells. This allows dynamic data input/output for the web automation.
  4. Looping:
    • Iterate through Excel rows to perform tasks on multiple entries.

Notes:

  • Update the FindElementById attributes to match the actual element IDs from the webpage you are automating.
  • Ensure your browser driver matches your browser version.
  • Save your Excel file as a macro-enabled workbook (.xlsm) to run VBA macros.

Let me know if you need additional features or refinements!

RKsTechAdemy 5 February 2025
Share this post
Archive
Sign in to leave a comment
Automating Form Submission Using Selenium in Excel VBA