html.getElementsByClassName
返回元素的集合,而不是单个元素,您应该检查该集合是否有任何项目,而不是直接检查它是否是Nothing
.。
Sub test()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim checkList As IHTMLElementCollection
Dim url As String
url = "https://stackoverflow.com/questions/79413010/vba-check-if-div-exists-or-not" ' Update this with your target URL
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate url
' Wait until IE is done loading page
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to " & url
DoEvents
Loop
Set html = ie.Document
' changed for testing purposes
Set checkList = html.getElementsByClassName("s-topbar--skip-link")
Check if any error notifications exist
If checkList.Length > 0 Then
MsgBox "Error notification found. Exiting the subroutine."
Exit Sub
Else
' Continue with your logic if no error notification is found
MsgBox "No error notifications detected. Continuing..."
' << Add your continuation logic here >>
End If
' Clean up
ie.Quit
Set ie = Nothing
Set html = Nothing
Set checkList = Nothing
End Sub