Working with Selenium on VBA

Working with Selenium on VBA

Generally, VBA correspond with Internet Explorer when it comes web automation. Selenium provides a way to communicate with other browsers like Chrome, Firefox, Edge etc.

This article will tell you how to setup Selenium to use with VBA.

Selenium provides easier way to communicate with Browser(s) and provide pre-defined methods and properties we can use to do web automation.

Installing Selenium Basic

Here is the link you can use the download the selenium Basic.

https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/selenium-vba/SeleniumWrapperSetup-1.0.17.0.exe

When we install Selenium Basic, it will also download the Web Driver, but when browser(chrome) get an update, we will need to download the latest web driver

Installing the latest web driver for chrome

Here is how you will know which version of web driver you need to download.

First, Check the version of chrome –

Selenium with VBA

Click on “About Google Chrome” in Chrome Menu – > Help menu, this will bring the next screen

Selenium with VBA

Now you see the chrome version you need to download the web driver for.

Now you visit the link here

https://sites.google.com/a/chromium.org/chromedriver/downloads

this link will take you this page showing below. Here you can download the matching version.

Selenium with VBA

Once the chromedriver.exe file is downloaded you need to put it in a special folder at this path.

C:\Users\YOURUSERNAME\appdata\local\seleniumbasic

Notice that, there will already be a chromedriver.exe file, you will need to replace that with new one.

At this point. Your installation is complete.

How to use Selenium with VBA.

Once selenium and web driver are ready. We will need to connect VBA with selenium library. This is how you will do it.

Open VBE -> Tools -> Reference

And select “Selenium Type Library” then hit Ok.

Once reference library is added, you can create object of any web browser you want.

In the example given below. This program will open google.com and search for itchat.in and will open itchat.in from the search results.

Sub StartChrome() 

 ‘– Declare webdriver variable
 Dim objChrome As WebDriver 

 ‘–Declare the elements we need
 Dim objSearchButton As WebElement
 Dim objSearchBox As WebElement
 Dim objLink As WebElement 

 ‘– Start Chrome with website
 Set objChrome = New WebDriver
 objChrome.Start “Chrome”

 ‘– Navigate the website we want
 objChrome.Get “https://www.google.com”
 objChrome.Wait 5000 

 ‘– Get object of SearchBox
 Set objSearchBox = objChrome.FindElementByClass(“gLFyf”)

 objSearchBox.Clear

 ‘–Enter value in Search box
 objSearchBox.SendKeys “www.itchat.in” 

 ‘–Get object of search button
 Set objSearchButton = objChrome.FindElementByClass(“gNO89b”)

 ‘–Click on search button
 objSearchButton.Submit 

 ‘–Get object of first link
 Set objLink = objChrome.FindElementByClass(“LC20lb”)
 ‘–Click on the first link
 objLink.Click
End Sub

Once your program ends, it will also end the chrome session. You can do almost everything by using web driver properties and methods. Like running java scripts, scroll to pages, take screenshot, save screenshots.

This is awesome, isn’t it? Tell me in comments below.

Excel VBA WhatsApp Group – Get Instant Help

Excel VBA WhatsApp Group is here to help you instantly with your Excel VBA queries. You can anytime ask anything like formulas, VBA, Charts. We will help you with that. Spammers are not welcome and we will remove them instantly.

Fill the userform showing below only on your mobile phone to join the whatsapp group for excel, but if are using Web.WhatsApp.com you can use your computer. After you fill the form it will redirect you to the group chat.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *