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.
To Automate Chrome browser with Excel VBA, first step is to download and install Selenium 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
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:
4. Setup Chromedrver Folder
Extract Chromedriver.exe from Zip file downloaded in previous step & replace the chromedriver.exe in below path.
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 Selenium in Excel VBA editor
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.
Install Selenium for Excel VBA – 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.