When it comes to web interactions (e.g. downloading and uploading data), Selenium is the king!
Indeed there are other ways to achieve the same thing, but Selenium is helpful especially when you need to interact with an website for a long period of time and that it doesn’t have an API you can access. This is because huge websites like Facebook, Instagram, Google, Amazon, YouTube and so on have systems to detect robots, therefore you need an active browser profile to interact with them. Once you get familiar with Selenium, you can work with almost any type of website and you can pass your work to little robots you’re going to create.
So, on short, Selenium is a piece of software that allows you to access browsers through coding. It finds and interacts with a webpage mostly through targeting HTML elements* (more about this in the next post).
Note*: It can also be done through raw mouse movement, but that is not a solution I would recommend!
But before we’re going to download and install Selenium, make sure you have Chrome browser installed. We are going to use it together with Selenium to make a little robot.
Okay, now you’re ready.
Step 1: Click on this link. It should automatically download Selenium;
Step 2: Go through the installation process;
Step 3: Uncheck the box Install the Selenium IDE Addon for Firefox because we’re going to use Chrome.
Now you should have Selenium Basic installed in your computer. The folder should be in a location similar with this one -> C:\Users\<username>\AppData\Local\SeleniumBasic
We have the software installed, but we also need a connection between Selenium and the browser, in our case, Chrome. This is done through a driver and this driver, for Chrome, is called chromedriver (oh, how surprising!).
For this connection to work, we need to download the chromedriver, but this driver is specific to the Chrome version we have installed, therefore we firstly need to check this.
Step 1: Open Chrome;
Step 2: Enter this as url -> chrome://version/
The version should be stated in the first row, as it can be seen in the image below.
Once you have the version, go to the chromedriver download page and download the corresponding version (it doesn’t need to be exact match for all those numbers; the first part is enough).
In my case, it will be the version 96. After clicking the link, you will have a window where you need to choose the version. As stated in the main page of this tutorial, the things I’m showing here are for windows, therefore you need to download the windows version.
After downloading, extract the file chromedriver.exe and move it to the main folder of Selenium (e.g. C:\Users\<username>\AppData\Local\SeleniumBasic). It will ask you to confirm the replacement. Do it! Please do it!
Note: This replacement is asked because at the time when this version of Selenium has been developed, the developer provided the drivers that were compatible at that point in time. Meanwhile the browsers have been updated, therefore new drivers are required, so the old ones need to be replaced.
Now we are ready to start coding!
Let’s open a new Excel file and create a blank macro named launch_selenium (PS: if you don’t know how to do this, please go through the part 1 of this VBA tutorial).
Even though we have everything installed in our computer, we firstly need to connect Selenium with our Excel file that will hold the macro(s). To do this, go to Tools -> References and in the newly opened window search for Selenium Type Library. It should be down the line at S.
To add it, check the box next to it and click OK.
Once this is done, let’s write the first lines that will launch Chrome through Selenium.
Set driver = New Webdriver driver.Start "Chrome"
If you run the macro, a new Chrome window should appear and it will shortly close. Don’t panic! That’s normal. Let’s add a Stop at the end of the macro so it stops and the window won’t close.
Set driver = New WebDriver driver.Start "Chrome" Stop
Now, the window should stay open and you shouldn’t receive any errors. To exit the stopped macro, go into the code window and hit F5 or click the Run button located on top, as shown below.
To make the driver go to an url, just add driver.get followed by the desired url.
Congratulations! We just created a simple Selenium-based macro! Full code below!
Other posts in this series -> VBA Tutorial for Beginners
PS: If you want to support this website, you can do it through the Show Your Support page!
Sub launch_selenium() Set driver = New WebDriver driver.Start "Chrome" driver.Get "http://www.google.com" Stop End Sub