florentbr/SeleniumBasic

How to check HTTP response?

allwynjose opened this issue · 3 comments

I have a list of links in excel. I need validate each link for responsiveness / broken link, etc.
It's an internal website, and i tried the usual macro function but it returns 200 mostly.
My question is, is it possible with selenium basic (in VBA) to navigate to a url, get it's HTTP response code (200, 401, 403, etc..) before closing the page? Any ideas would be appreciated. Thanks!

I usually just ping website to see if it is responding

`
Function PingOK() As Boolean

Dim objPing As Object
Dim objItem As Object
Dim done As Boolean
Dim cnt As Integer

done = False
cnt = 1
PingOK = False

Do While Not done And cnt <= 4
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
ExecQuery("select * from Win32_PingStatus " & _
"where address = '" & "www.google.com" & "'")

cnt = cnt + 1
              
For Each objItem In objPing
  If objItem.StatusCode = 0 Then
    PingOK = True
    done = True
  End If
Next objItem

Loop
End Function
`

Gby3 commented

Mogulman52 provided you a good alternative there. If you still want to use selenium for the purpose i suggest you checking the presence of some element showing up only when the page is loaded correctly.
This can be done using the FindBy function or via javascript:
driver.executescript("return yourelementlocatorhere[i].length")
If returns 0 your element wasn't found and means the link is not working.

Thank you @mogulman52 .. that did the trick!