Skip to Content

Automating Form Submission Using Selenium in Excel VBA

Introduction

Automating form submissions using Excel VBA and Selenium can save time and reduce manual data entry errors. Whether you're filling out login forms, customer information, or online applications, VBA with Selenium allows you to interact with websites efficiently.

This guide covers:

✔️ Setting up Selenium with Excel VBA

✔️ Automating form submission using VBA

✔️ Handling dynamic elements and dropdowns

✔️ Error handling for smooth execution


1️⃣ Setting Up Selenium for Excel VBA

📍 Step 1: Install SeleniumBasic

SeleniumBasic is required to automate web browsers using VBA.

  1. Download SeleniumBasic from GitHub.
  2. Install it (default location: C:\Program Files\SeleniumBasic).

📍 Step 2: Download and Set Up ChromeDriver

ChromeDriver is needed for automation in Chrome.

  1. Visit Chrome for Testing.
  2. Download the correct ChromeDriver version for your Chrome browser.
  3. Copy chromedriver.exe to C:\Program Files\SeleniumBasic.

📍 Step 3: Enable Selenium in VBA

  1. Open Excel and press ALT + F11 to open the VBA Editor.
  2. Go to Tools > References.
  3. Find and check Selenium Type Library.
  4. Click OK to enable Selenium.


2️⃣ Automating Form Submission Using VBA

Let’s automate a login form submission using VBA and Selenium.

📍 Example: Automating Login to a Website

Sub AutomateLogin()
    Dim driver As New Selenium.WebDriver

    ' Start Chrome and open the login page
    driver.Start "chrome"
    driver.Get "https://example.com/login"

    ' Enter Username
    driver.FindElementById("username").SendKeys "your_username"

    ' Enter Password
    driver.FindElementById("password").SendKeys "your_password"

    ' Click Login Button
    driver.FindElementByXPath("//button[text()='Login']").Click

    Application.Wait Now + TimeValue("00:00:05") ' Wait for login to complete

    MsgBox "Login successful!", vbInformation, "Automation Complete"

    driver.Quit
End Sub

🔹 What This Code Does:

Opens Chrome and navigates to the login page

Fills in username and password fields

Clicks the login button

Waits for the page to load before proceeding

Closes the browser after submission


3️⃣ Automating a Multi-Field Form Submission

Now, let’s automate a more complex form with multiple input fields, checkboxes, radio buttons, and dropdowns.

📍 Example: Filling and Submitting a Registration Form

Sub SubmitRegistrationForm()
    Dim driver As New Selenium.WebDriver

    ' Open Chrome and navigate to the form page
    driver.Start "chrome"
    driver.Get "https://example.com/register"

    ' Enter text fields
    driver.FindElementById("first_name").SendKeys "John"
    driver.FindElementById("last_name").SendKeys "Doe"
    driver.FindElementById("email").SendKeys "johndoe@example.com"

    ' Select Gender (Radio Button)
    driver.FindElementByXPath("//input[@value='Male']").Click

    ' Select Country from Dropdown
    Dim countryDropdown As WebElement
    Set countryDropdown = driver.FindElementById("country")
    countryDropdown.AsSelect.SelectByVisibleText "India"

    ' Check a Checkbox (Agree to Terms)
    driver.FindElementById("agree_terms").Click

    ' Click Submit Button
    driver.FindElementById("submit_button").Click

    Application.Wait Now + TimeValue("00:00:05") ' Wait for submission

    MsgBox "Form submitted successfully!", vbInformation, "Success"

    driver.Quit
End Sub

🔹 What This Code Does:

Enters first name, last name, and email

Selects a radio button for gender

Chooses a country from a dropdown

Checks the "Agree to Terms" checkbox

Submits the form


4️⃣ Handling Dropdowns in Selenium VBA

Dropdowns require special handling in Selenium VBA. Use the AsSelect.SelectByVisibleText method to select an option.

📍 Example: Selecting from a Dropdown List

Dim country As WebElement
Set country = driver.FindElementById("country")
country.AsSelect.SelectByVisibleText "United States"

Alternatively, select an option by index:

country.AsSelect.SelectByIndex 2 ' Selects the third option


5️⃣ Handling Dynamic Elements (JavaScript-Rendered Content)

Sometimes, form fields appear after user interaction. Use Wait commands to handle these situations.

📍 Example: Waiting for a Field to Appear

Dim field As WebElement
Set field = driver.FindElementByXPath("//input[@id='dynamic_field']", timeout:=10)
field.SendKeys "Dynamic Data"

🔹 This waits up to 10 seconds for the field to appear before entering data.


6️⃣ Error Handling in Form Automation

Web page structures change frequently. If an element is missing, your script might crash. Use error handling to prevent this.

📍 Example: Handling Missing Elements

On Error Resume Next
Set field = driver.FindElementById("email")
If Err.Number <> 0 Then
    MsgBox "Email field not found!", vbCritical, "Error"
    driver.Quit
    Exit Sub
End If
On Error GoTo 0

🔹 This prevents the script from crashing if an element is missing.


7️⃣ Automating File Upload in Selenium VBA

If a form includes a file upload, we need to send the file path directly to the input field.

📍 Example: Uploading a File

driver.FindElementById("upload").SendKeys "C:\Users\JohnDoe\Documents\resume.pdf"

🔹 This bypasses the file selection dialog and uploads the file automatically.


Conclusion

With Excel VBA and Selenium, you can:

Automate login and form submission

Handle checkboxes, radio buttons, and dropdowns

Manage dynamic content with wait functions

Use error handling for smooth execution

Upload files automatically


This method helps streamline data entry tasks, making repetitive online form submissions faster and error-free! 🚀


RKsTechAdemy 10 March 2025
Share this post
Archive
Sign in to leave a comment
Web Scraping with Excel VBA and Selenium