laptop

‘Dim HTMLDoc As Object
Dim HTMLDoc As MSHTML.HTMLDocument
Dim currentElement As MSHTML.IHTMLElement
‘Dim HTMLDoc As MSHTML.HTMLDocument
Dim bodyElement As MSHTML.IHTMLElement
Dim iElement As MSHTML.IHTMLElement
Private Sub UserForm_Initialize()
‘ Set the initial URL to Google search
WebBrowser1.Navigate “https://www.google.com/search?q=indian train”
End Sub

Private Sub CommandButton1_Click()
‘ Navigate to the desired web page
‘WebBrowser1.Navigate “https://www.example.com” ‘ Change this to your target URL

' Wait for the page to load completely
Do While WebBrowser1.Busy Or WebBrowser1.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Loop

' Get the HTML document
Dim HTMLDoc As MSHTML.HTMLDocument
Set HTMLDoc = WebBrowser1.Document

' Clear previous results in ListBox
ListBox1.Clear

' Get all elements in the document
Dim allelements As MSHTML.IHTMLElementCollection
Set allelements = HTMLDoc.getElementsByTagName("*") ' * gets all elements

Dim htmlElement As MSHTML.IHTMLElement
For Each htmlElement In allelements
    ' Display element tag name, ID, class, and innerText in the ListBox
    Debug.Print "Tag: " & htmlElement.tagName _
                     & ", ID: " & htmlElement.ID _
                     & ", Class: " & htmlElement.className _
                     & ", Text: " & htmlElement.innerText
Next htmlElement

End Sub

Function eDetail()
Dim i As Long
For i = 0 To iElement.Children.Length – 1
Set currentElement = iElement.Children(i)
Debug.Print “Child ” & i & “: ” & currentElement.tagName & ” (Children count: ” & currentElement.Children.Length & “)”
Next i
End Function

Private Sub CommandButton2_Click()
‘ Navigate to the desired web page
‘WebBrowser1.Navigate “https://www.example.com” ‘ Replace with your target URL

' Wait for the page to load completely
Do While WebBrowser1.Busy Or WebBrowser1.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Loop

' Get the HTML document

Set HTMLDoc = WebBrowser1.Document

' Start by getting the body element

Set bodyElement = HTMLDoc.getElementsByTagName("body")(0)

If bodyElement Is Nothing Then
    MsgBox "Body element not found."
    Exit Sub
End If

' Debug: Print body element
Debug.Print "Body found: " & bodyElement.tagName

' Iterate over all children of the body

‘ For i = 0 To bodyElement.Children.Length – 1
‘ Set currentElement = bodyElement.Children(i)
‘ Debug.Print “Child ” & i & “: ” & currentElement.tagName & ” (Children count: ” & currentElement.Children.Length & “)”
‘ Next i
Set iElement = bodyElement
Call eDetail

' Step 1: /html/body/div[4]
Set currentElement = bodyElement.Children(1)
If currentElement Is Nothing Then
    MsgBox "Step 1: div[4] not found."
    Exit Sub
End If
'Debug.Print "Step 1: " & currentElement.tagName

Set iElement = currentElement
Call eDetail
'/html/body/div[4]/div/div[13]/div[1]/div[2]/div[2]/div/div/div[4]
' Step 2: /div
Set currentElement = currentElement.Children(0)

If currentElement Is Nothing Then
    MsgBox "Step 2: First child div not found."
    Exit Sub
End If

‘ Debug.Print “Step 2: ” & currentElement.tagName
Set iElement = currentElement
Call eDetail

' Step 3: /div[13]
Set currentElement = currentElement.Children(12)
If currentElement Is Nothing Then
    MsgBox "Step 3: div[13] not found."
    Exit Sub
End If
Debug.Print "Step 3: " & currentElement.tagName

' Step 4: /div[1]
Set currentElement = currentElement.Children(0)
If currentElement Is Nothing Then
    MsgBox "Step 4: div[1] not found."
    Exit Sub
End If
Debug.Print "Step 4: " & currentElement.tagName

' Step 5: /div[2]
Set currentElement = currentElement.Children(1)
If currentElement Is Nothing Then
    MsgBox "Step 5: div[2] not found."
    Exit Sub
End If
Debug.Print "Step 5: " & currentElement.tagName

' Step 6: /div[2]
Set currentElement = currentElement.Children(1)
If currentElement Is Nothing Then
    MsgBox "Step 6: div[2] not found."
    Exit Sub
End If
Debug.Print "Step 6: " & currentElement.tagName

' Step 7: /div
Set currentElement = currentElement.Children(0)
If currentElement Is Nothing Then
    MsgBox "Step 7: First child div not found."
    Exit Sub
End If
Debug.Print "Step 7: " & currentElement.tagName

' Step 8: /div
Set currentElement = currentElement.Children(0)
If currentElement Is Nothing Then
    MsgBox "Step 8: First child div not found."
    Exit Sub
End If
Debug.Print "Step 8: " & currentElement.tagName

' Step 9: /div[4]
Set currentElement = currentElement.Children(3)
If currentElement Is Nothing Then
    MsgBox "Step 9: div[4] not found."
    Exit Sub
End If
Debug.Print "Step 9: " & currentElement.tagName

' Display the inner text or HTML of the target element
MsgBox "Found element: " & vbCrLf & currentElement.innerText

End Sub

Loading

Scroll to Top