Excel VBA to Automate Chrome using Selenium
In this topic we will learn how to install Selenium library in Windows PC & initiate Chrome browser.
Selenium is a free open source framework that help in testing & automating web applications in browser.
Many programmers use this for automated web data extraction like stock quotes, sports betting , new updates, social media feeds, public directories etc., that refresh data periodically.
Steps to add Selenium type library to Excel VBA for Windows PC
Follow this step by step process carefully to learn Web Data extraction basics.
If you miss even a tiny detail, the process might fail to launch Chrome or extract data from the web browser.
1.Selenium VBA Download
To Automate Chrome browser with Excel VBA, first step is to download and install Selenium type library from this page:
Download the latest version & the one that is compatible for your Windows OS type. Install this to add Selenium library for Excel VBA.
2. Google Chrome Browser Version
From Chrome settings, check for version of the browser installed (chrome://settings/help)
- Sample: Version 91.0.4472.77 – Official Build 64-bit
It is a older version mentioned above. In your PC, it could be a advanced version.
3. Chromedriver Download
The next and final software required for Selenium to to work is Chromedriver.
Go to this Google sites page and download version Zip version of Chromedriver relative to the Chrome installed in your pc:
Do not run the exe file inside this zip folder. Follow the steps explained below.
4. Setup Chromedrver Folder
Extract Chromedriver.exe from Zip file downloaded in previous step & replace the chromedriver.exe in below path.
- C:\Users\%username%\AppData\Local\SeleniumBasic
A file with same name might be present already in this folder. Rename it to Chromedriverbkp.exe and then extract the latest file from zip to this folder.
Add Selenium Reference to Excel VBA
The installation step is done. Selenium library is installed in your PC and should be available within Excel VBA environment.
Lets find it out whether everything went fine till now.
5. Add reference to Excel VBA Selenium Type Library
Open a Excel file, save it as .xlsm file, press Alt + F11 to view VBA editor. Then add selenium to Excel VBA project from this menu option.
- Tools – References – Selenium Type Library
6. Sample Code – Excel VBA Selenium Chrome
Add a new module, copy paste this code to EXcel VBA editor. When you run the code, you wil be able to see a Chrome browser is getting opened, opens the url & then gets closed.
Sub InitBrowser() Dim objChrome As New Chromedriver objChrome.SetProfile "C:\Users\<getUserName>\AppData\Local\Google\Chrome\User Data\Default" objChrome.Start "chrome", "https://www.google.com" objChrome.Get "/" End Sub
Note: If you would like the browser to stay after the code gets executed, define the “objChrome” as a global variable outside the function.
How to Install Excel VBA Selenium Type Library – Tutorial
Here is a video tutorial that explains all these steps in detail.
Also, you can choose to mention the username for the profile manually or make the VBA to get the active username and will it automatically.
This is a basic step for extracting webdata using Chrome using Excel VBA. In case you would like to extract web data without using Chrome for simple static pages, use this code.
Once you understand this basic concepts, then from here on it is easy to get the HTML code of any web page. Then you can use the built in functions available within Selenium type library & prase the data to Excel worksheet.
It is also possible to send data back to Chrome. Therefore it is easy to automate a whole lot of operations like Form filling, clicking a button, apply search criteria filters in any webpage. So, overall, selenium is a great option to automate chrome and alos other browsers like Firefox & Edge.