Skip to Content

How to set-up Selenium drive for VBA Web Automation


1. Install Selenium Basic

Selenium Basic is a wrapper that allows you to use Selenium with VBA.

  1. Download Selenium Basic:
  2. Install Selenium Basic:
    • Run the downloaded .exe file.
    • Follow the installation instructions.
    • By default, Selenium Basic will be installed in:
      C:\Users\<YourUsername>\AppData\Local\SeleniumBasic.

2. Download ChromeDriver

You need a driver compatible with your Chrome browser version.

  1. Determine Your Chrome Version:
    • Open Chrome and go to chrome://settings/help.
    • Note the version number (e.g., 116.0.5845.96).
  2. Download the Compatible ChromeDriver:
  3. Place the ChromeDriver:
    • Extract the downloaded ZIP file.
    • Copy the chromedriver.exe file.
    • Place it in the Selenium Basic installation folder:
      C:\Users\<YourUsername>\AppData\Local\SeleniumBasic.

3. Add Selenium Basic to VBA References

  1. Open Excel or any Office application that supports VBA (e.g., Word or Access).
  2. Open the VBA editor (Alt + F11).
  3. Go to Tools > References.
  4. Look for Selenium Type Library in the list.
  5. Check the box and click OK.

4. Verify the Installation

To confirm everything is set up correctly:

  1. Open the VBA editor and create a new module.
  2. Paste the following test code:
    Sub TestSelenium()
        Dim driver As New WebDriver
        driver.Start "chrome"
        driver.Get "https://www.google.com"
        MsgBox "Google has been opened!"
        driver.Quit
    End Sub
    
  3. Run the macro (F5).
  4. If Chrome opens Google, the setup is successful.


Code Breakdown:

Sub TestSelenium()
  • Purpose: This line starts the subroutine named TestSelenium.
  • A subroutine in VBA is a block of code that performs a specific task.
Dim driver As New WebDriver
  • Purpose: Declares a variable driver as a new instance of the Selenium WebDriver class.
  • WebDriver is the main interface in Selenium Basic used to control and interact with web browsers.
driver.Start "chrome"
  • Purpose: Starts a new browser session in Google Chrome.
  • "chrome" specifies the browser to use. Selenium Basic supports multiple browsers like Chrome, Firefox, and Edge.
  • Behind the Scenes:
    • Selenium Basic interacts with the ChromeDriver.exe file installed earlier.
    • It launches Chrome using the chromedriver.exe.
driver.Get "https://www.google.com"
  • Purpose: Navigates the browser to the specified URL, in this case, Google's homepage (https://www.google.com).
  • The Get method loads the webpage by sending an HTTP GET request to the URL.
MsgBox "Google has been opened!"
  • Purpose: Displays a message box to the user with the text "Google has been opened!".
  • This is used to confirm that the script has successfully opened the Google website.
driver.Quit
  • Purpose: Closes the browser and terminates the WebDriver session.
  • Why Important?: Ensures that the browser process does not remain running in the background, which can consume system resources.
End Sub
  • Purpose: Marks the end of the TestSelenium subroutine.

Execution Flow:

  1. Initialize: Create a new instance of the WebDriver.
  2. Launch Chrome: Open a new Chrome browser session.
  3. Navigate: Load the Google homepage.
  4. Notify User: Display a confirmation message to the user.
  5. Cleanup: Close the browser and end the WebDriver session.

Expected Output:

  1. Chrome browser opens automatically.
  2. It navigates to Google’s homepage.
  3. A message box appears with the text: "Google has been opened!".
  4. When you close the message box, the Chrome browser session also closes.

RKsTechAdemy 17 January 2025
Share this post
Archive
Sign in to leave a comment
Web Automation using Excel VBA Selenium