Do you want to use Selenium in VBA to automate testing and scraping but don’t know how to install and work with it?
This tutorial will guide you to install Selenium WebDriver for Windows, as well as help you understand how to work with Selenium in Excel VBA Programming.
So, let’s begin!
Table of Contents
- What is Selenium?
- Download the Selenium Executable file for Windows
- Download Webdriver for your browser
- Add Selenium in VBA
- Open Chrome Browser using VBA code
- Run Selenium Script
What is Selenium?
Selenium is an open-source tool that automates web browsers. It provides a single interface that lets you write test/scraping/automation/bot scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C #, among others.
It is possible to automate web browsing using Excel and VBA code. We can use Selenium in VBA for
- Automate repetitive web browser tasks.
- Quickly fill a web form multiple times with an Excel data set.
- Extract data from a web page and save it in an Excel sheet.
- Run web tests against an Excel data set (Data-Driven Testing).
- Take screenshots of a Website and save them in a PDF file.
Let’s start with step-by-step instructions to install Selenium for VBA.
Step 1: Download the Selenium Executable file for Windows
To begin using SeleniumBasic, first download SeleniumBasic-2.0.9.0.exe from https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0 as shown below screenshot and then run and install the downloaded .exe file.
After the installation is complete, a new folder will be created at C:\Users\[yourusername]\AppData\Local\SeleniumBasic. Note that [yourusername] refers to your Windows active user name. Keep this path in mind, as it will be required for a later step.
Step 2: Download Webdriver for your browser
After successfully installing Selenium, you’ll need to obtain the appropriate webdriver for your browser. Each browser requires a specific driver, such as chromedriver for Chrome, geckodriver for Firefox, and edgedriver for Edge. For this tutorial, we will use chromedriver for Chrome.
To determine the version of your Chrome browser, navigate to the menu and select Help -> About Google Chrome as shown in the below screenshot. Take note of the version number displayed. For example, 112.0.5615.121.
Visit the Chromedriver website https://chromedriver.chromium.org/downloads and locate the appropriate version of chromedriver that matches the major version of your Chrome browser. For the given Chrome version, we would need ChromeDriver 112.0.5615.49.
Click on the link for ChromeDriver 112.0.5615.49 and download the chromedriver_win32.zip file as shown in the below image.
For Chrome version 115 or newer, click on the below-marked link which contains chromedriver for a newer version of Chrome.
Then click on Stable version of Chromedriver as shown in the below image.
After this, a new page will open. You need to check what your operating system bit, 32bit or 64bit. If it is x64 then copy the URL for win64 chromedriver and open it. The chromedriver will download for your specified Chrome.
Once the download is complete, extract the contents of the zip file. You should find a chromedriver.exe file inside this zip file.
Copy this file and paste it into the C:\Users\[yourusername]\AppData\Local\SeleniumBasic folder, which is where we previously installed Selenium Basic.
Step 3: Add Selenium in VBA
Create a new Excel file and save as it an Excel Macro-Enable Workbook with the name TestSelenium.xlsm. Take note that Excel Macro-Enable has an extension .xlsm.
Click on the Developer menu then click Visual Basic or Press Alt+F11 to open the VBA editor.
If you are unable to see the Developer tab in Excel Menu then you can enable it using the following steps:
- In the Excel menu, click File->Options.
- Select Customize Ribbon from the left sidebar.
- Now check the Developer option from Customize the Ribbon list box which is on the right side of the dialog box.
In VBA Editor, click Tools->References… menu, and it will open a new dialog window. Scroll down in the list and find Selenium Type Library. Click the checkbox to check, then click the OK button as shown in the figure.
Now you have successfully added Selenium to your current Excel workbook.
Step 4: Open Chrome Browser using VBA code
Now you need to create a new module in VBA Editor. Click menu Insert->Module. It will add a new module Module1.
Now, double-click on the newly created Module1 to create a procedure/macro that will open Chrome Browser using Selenium. Add the below code to perform this action:
Dim driver As New WebDriver
Sub OpenBrowser()
driver.Start "Chrome"
driver.Get "https://www.vpktechnologies.com/"
End Sub
Save the code by pressing Ctrl+S and Close VBA Editor. Then go to your TestSelenium.xlsm Excel file.
Add a new button from the developer tab in Excel. The steps are shown below image.
Right-click on the newly added button then click Edit Text to rename the button text. Rename it to Open Browser.
Now we need to assign macro OpenBrowser() which we have created in VBA Editor. Right-click on the button and click on Assign Macro…
You will see our previously created VBA macro OpenBrowser() in the list. Select it and click the OK button.
Step 5: Run Selenium Script
All steps are done, now you can open Chrome Browser by clicking the Open Browser button.
If you want to close this Chrome browser by VBA code then you can use:
driver.Quit
Set driver = Nothing
Related: See our guide on How to Send a Connect Request in LinkedIn using Selenium VBA? for more details.
Related: See our guide on How to Send Messages on LinkedIn using Selenium VBA? for more details.
Selenium can be installed in different computer programs like Python. If you want to learn how to install Selenium in Python, you can check out the tutorial How to Install Chrome WebDriver in Selenium Python.