Selenium VBA Excel Tutorial (Chrome Web Scraping)

โšก Smart Summary

Selenium VBA in Excel automates Google Chrome to scrape data from HTML web pages, using the Selenium type library and WebDriver methods to open a site, locate table elements by class and tag, and write the results into worksheet cells.

  • ๐Ÿงฐ Selenium WebDriver: VBA uses the Selenium type library to drive Google Chrome for scraping.
  • ๐Ÿ“š Type Library: The Selenium type library must be referenced before WebDriver objects can be declared.
  • ๐Ÿš€ Start and Get: The Start method opens Chrome and the Get method loads the target website URL.
  • ๐Ÿ”Ž Element Locators: FindElementByClass and FindElementsByTag locate the table header and data cells.
  • ๐Ÿ“Š Write to Cells: A nested For Each loop writes header and row values into worksheet cells.
  • ๐Ÿค– AI Assistance: AI copilots help write, debug, and maintain Selenium VBA scripts as sites change.

Selenium VBA Excel Tutorial

What is Data Scraping Using Selenium?

Selenium is an automation tool that facilitates scraping information from HTML web pages, performing web scraping with Google Chrome. In Excel, the Selenium type library lets VBA drive a real browser instead of the deprecated Internet Explorer engine.

How to Prepare the Excel Macro Before Data Scraping Using Selenium

There are certain prerequisites that must be performed on the Excel macro file before starting the data scraping process.

These prerequisites are as follows:

Step 1) Open an Excel-based macro and access the Developer option of Excel.

Prepare Excel Macro before Data Scraping using Selenium

Step 2) Select the Visual Basic option under the Developer ribbon.

Prepare Excel Macro before Data Scraping using Selenium

Step 3) Insert a new module.

Prepare Excel Macro before Data Scraping using Selenium

Step 4) Initialize a new subroutine and name it test2.

Sub test2()
End Sub

The result in the module would be as follows:

Prepare Excel Macro before Data Scraping using Selenium

Step 5) Access the Reference option under the Tools tab and reference the Selenium Type Library. This library helps open Google Chrome and facilitates macro scripting.

Reference Selenium Type Library in Excel VBA

Now the Excel file is ready to interact with the browser. The next step is to incorporate a macro script that facilitates data scraping in HTML.

How to Open Google Chrome Using VBA

Here are the steps to open Google Chrome using VBA.

Step 1) Declare and initialize the variables in the subroutine as shown below.

Sub test2()
Dim driver As New WebDriver
Dim rowc, cc, columnC As Integer

Step 2) To open Google Chrome using Selenium and VBA, write driver.Start “Chrome” and press F5.

The following would be the code.

Sub test2()
Dim driver As New WebDriver
Dim rowc, cc, columnC As Integer
driver.Start "Chrome"
Application.Wait Now + TimeValue("00:00:20")
End Sub

The module would result as follows:

Open Google Chrome using VBA

How to Open a Website in Google Chrome Using VBA

Once you can access Google Chrome using VBA, the next step is to access a website using VBA. This is facilitated by the Get function, where the URL is passed as double quotes.

The module would look as follows:

Open Website in Google Chrome using VBA

Press F5 to execute the macro. The following webpage would open in Google Chrome as shown.

Sub test2()
Dim driver As New WebDriver
Dim rowc, cc, columnC As Integer
driver.Start "Chrome"
driver.Get "https://demo.guru99.com/test/web-table-element.php"
Application.Wait Now + TimeValue("00:00:20")
End Sub

Open Website in Google Chrome using VBA

Now the Excel macro is ready to perform the scraping tasks. The next step shows how the information can be extracted by applying Selenium and VBA.

How to Scrape Information from a Website Using VBA

Suppose the day trader wants to access the data from the website daily. Each time the day trader clicks the button, it should auto-pull the market data into Excel.

From the above website, it is necessary to inspect an element and observe how the data is structured. Access the source code of the HTML below by pressing Control + Shift + I.

<table class="datatable">
<thead>
<tr>
<th>Company</th>
<th>Group</th>
<th>Pre Close (Rs)</th>
<th>Current Price (Rs)</th>
<th>% Change</th>
</tr>

As you can see, the data is structured as a single HTML table. Therefore, to pull the entire data from the HTML table, you must design a macro that pulls the header information and the corresponding data associated with the table. Perform the tasks below.

Step 1) Formulate a For loop that runs through the HTML header information as a collection. The Selenium driver locates the header information using the FindElementByClass() and FindElementByTag() methods, as shown.

Sub test2()
Dim driver As New WebDriver
Dim rowc, cc, columnC As Integer
rowc = 2
Application.ScreenUpdating = False
driver.Start "chrome"
driver.Get "https://demo.guru99.com/test/web-table-element.php"
For Each th In driver.FindElementByClass("dataTable").FindElementByTag("thead").FindElementsByTag("tr")
cc = 1
For Each t In th.FindElementsByTag("th")
Sheet2.Cells(1, cc).Value = t.Text
cc = cc + 1
Next t
Next th

Step 2) Next, the Selenium driver locates the table data using a similar approach. Write the following code:

Sub test2()
Dim driver As New WebDriver
Dim rowc, cc, columnC As Integer
rowc = 2
Application.ScreenUpdating = False
driver.Start "chrome"
driver.Get "https://demo.guru99.com/test/web-table-element.php"
For Each th In driver.FindElementByClass("dataTable").FindElementByTag("thead").FindElementsByTag("tr")
cc = 1
For Each t In th.FindElementsByTag("th")
Sheet2.Cells(1, cc).Value = t.Text
cc = cc + 1
Next t
Next th
For Each tr In driver.FindElementByClass("dataTable").FindElementByTag("tbody").FindElementsByTag("tr")
columnC = 1
For Each td In tr.FindElementsByTag("td")
Sheet2.Cells(rowc, columnC).Value = td.Text
columnC = columnC + 1
Next td
rowc = rowc + 1
Next tr
Application.Wait Now + TimeValue("00:00:20")
End Sub

Excel can be initialized using the Range attribute or the Cells attribute of the sheet. To reduce the complexity of the VBA script, the collection data is initialized to the Cells attribute of Sheet 2 in the workbook. The Text attribute helps get the text placed under an HTML tag.

Sub test2()
Dim driver As New WebDriver
Dim rowc, cc, columnC As Integer
rowc = 2
Application.ScreenUpdating = False
driver.Start "chrome"
driver.Get "https://demo.guru99.com/test/web-table-element.php"
For Each th In driver.FindElementByClass("dataTable").FindElementByTag("thead").FindElementsByTag("tr")
cc = 1
For Each t In th.FindElementsByTag("th")
Sheet2.Cells(1, cc).Value = t.Text
cc = cc + 1
Next t
Next th
For Each tr In driver.FindElementByClass("dataTable").FindElementByTag("tbody").FindElementsByTag("tr")
columnC = 1
For Each td In tr.FindElementsByTag("td")
Sheet2.Cells(rowc, columnC).Value = td.Text
columnC = columnC + 1
Next td
rowc = rowc + 1
Next tr
Application.Wait Now + TimeValue("00:00:20")
End Sub

The VBA module would look as follows:

Scrape Information from Website using VBA

Step 3) Once the macro script is ready, assign the subroutine to an Excel button and exit the VBA module. Label the button as Refresh or any suitable name. For this example, the button is labeled Refresh.

Scrape Information from Website using VBA

Step 4) Press the Refresh button to get the output below.

Scrape Information from Website using VBA

Step 5) Compare the results in Excel with the results in Google Chrome.

Scrape Information from Website using VBA

FAQs

Excel needs the Selenium Type Library, installed through SeleniumBasic, referenced in the VBA editor under Tools, References. This library provides the WebDriver object and methods such as Start, Get, and FindElementByClass used to automate the browser.

Selenium VBA can drive Google Chrome, Mozilla Firefox, Microsoft Edge, and Internet Explorer. Chrome is the most common choice. Each browser needs its matching WebDriver, and the same VBA code can switch browsers with minimal changes.

The Internet Explorer method uses the HTML Object Library and only IE, which is deprecated. Selenium drives modern browsers such as Chrome and offers multi-browser support and more reliable element location.

Yes. AI copilots can generate Selenium VBA code from a description, suggest element locators, and help debug errors. When a website changes, AI can propose updated selectors, reducing the manual effort of maintaining scraping macros.

AI adds intelligent element detection, natural-language commands, and self-healing locators to browser automation. These machine learning features let tools adapt to layout changes automatically, making scraping and testing more resilient than fixed, hard-coded scripts.

Summarize this post with: