Sub WebAutomationSelenium()
Dim bot As New WebDriver
Dim Table As Object, Rows As Object, Row As Object, Cells As Object, Cell As Object
Dim i As Integer, j As Integer
' Initialize ChromeDriver
bot.Start "chrome", "https://demo.istat.it/app/?i=D7B&l=it&a=2024"
bot.Window.Maximize
' Wait for the page to load
bot.Wait 5000
' Click the "Vista Territoriale" button
If Not WaitForElement(bot, By.ID, "tab-1", 10) Then
MsgBox "Failed to find the 'Vista Territoriale' button.", vbExclamation
bot.Quit
Exit Sub
End If
bot.FindElementById("tab-1").Click
' Wait briefly for changes to take effect
bot.Wait 2000
' Select the radio button "Tutti i comuni della provincia selezionata"
If Not WaitForElement(bot, By.ID, "provincerb-1", 10) Then
MsgBox "Failed to find the 'Tutti i comuni' radio button.", vbExclamation
bot.Quit
Exit Sub
End If
bot.FindElementById("provincerb-1").Click
' Remove the disabled attribute from the province dropdown
bot.ExecuteScript "document.getElementById('province-1').removeAttribute('disabled');"
bot.Wait 1000
' Select "Como" from the province dropdown
Dim provinceDropdown As Object
Set provinceDropdown = bot.FindElementById("province-1")
For i = 0 To provinceDropdown.FindElementsByTag("option").Count - 1
If provinceDropdown.FindElementsByTag("option")(i).Text = "Como" Then
provinceDropdown.FindElementsByTag("option")(i).Click
Exit For
End If
Next
' Wait for changes to apply
bot.Wait 2000
' Select "Luglio" from the month dropdown
If Not WaitForElement(bot, By.ID, "mese", 10) Then
MsgBox "Failed to find the 'month' dropdown list.", vbExclamation
bot.Quit
Exit Sub
End If
Dim monthDropdown As Object
Set monthDropdown = bot.FindElementById("mese")
For i = 0 To monthDropdown.FindElementsByTag("option").Count - 1
If monthDropdown.FindElementsByTag("option")(i).Text = "Luglio" Then
monthDropdown.FindElementsByTag("option")(i).Click
Exit For
End If
Next
' Click the "Cerca" button
If Not WaitForElement(bot, By.ID, "btnricerca-0", 10) Then
MsgBox "Failed to find the 'Cerca' button.", vbExclamation
bot.Quit
Exit Sub
End If
bot.FindElementById("btnricerca-0").Click
bot.Wait 2000
' Locate the table element and extract data
Set Table = bot.FindElementById("result-table") ' Replace with actual table ID if different
If Table Is Nothing Then
MsgBox "Failed to find the result table.", vbExclamation
bot.Quit
Exit Sub
End If
Set Rows = Table.FindElementsByTag("tr")
' Loop through rows and cells to extract data
For i = 1 To Rows.Count - 1 ' Start at 1 to skip header row
Set Row = Rows.Item(i)
Set Cells = Row.FindElementsByTag("td")
For j = 0 To Cells.Count - 1
Set Cell = Cells.Item(j)
Debug.Print "Row " & i & ", Column " & j & ": " & Cell.Text
Next j
Next i
' Close ChromeDriver
bot.Quit
End Sub
' Helper function to wait for an element to load
Function WaitForElement(bot As WebDriver, selectorType As By, selector As String, maxSeconds As Integer) As Boolean
Dim secondsElapsed As Integer
secondsElapsed = 0
Do While bot.FindElement(selectorType, selector) Is Nothing
If secondsElapsed >= maxSeconds Then
WaitForElement = False
Exit Function
End If
secondsElapsed = secondsElapsed + 1
bot.Wait 1000 ' Wait 1 second
Loop
WaitForElement = True
End Function