Al F.
Al F.

Reputation: 57

How Do I Test If Webpage Contains Certain Text

I'm trying to detect if a web page has certain text. For example, I want to see if this web page includes the following phrase: "Here is my code"

I can't get it to ever find that the "If Then" condition is satisfied. Here's what I'm trying:

Const READYSTATE_COMPLETE = 4
Declare Function SetForegroundWindow Lib "user32" _
Alias "SetForegroundWindow" (ByVal Hwnd As Long)As Long
' Declare Internet Explorer object
Dim IE As SHDocVw.InternetExplorer
 Dim strProgramName As String

Sub Main

   ' create instance of InternetExplorer
   Set IE = New InternetExplorer
   ' using your newly created instance of Internet Explorer

  With IE
      SetForegroundWindow IE.HWND
      .Visible = True
      .Navigate2 "https://stackoverflow.com/questions/38355762/how-do-i-modify-web-scraping-code-to-loop-through-product-bullets-until-it-finds"

  ' Wait until page we are navigating to is loaded
      Do While .Busy
      Loop
      Do
      Loop Until .readyState = READYSTATE_COMPLETE
      On Error Resume Next
         If Err Then

         Else

  End If

  Wait 2


If InStr(IE.document.body.innerHTML, "Here is my code") > 0 Then

MsgBox "Yessiree Bob"

Else

MsgBox "The text dosen't exist"

End If

    Set IE = Nothing
   ' Tidy Up
End With
End Sub

I've also tried:

 FindText = InStr(1, IE.document.body.innerHTML, "Here is my code")

 If FindText > 0 Then

And

 msg = IE.document.body.innerHTML
 If InStr(msg, "Here is my code") > 0 Then

But nothing works. I've looked on Stack Overflow, but can't find this exact question.

Thanks in advance!

Upvotes: 2

Views: 1317

Answers (1)

RowanC
RowanC

Reputation: 1649

Use:

If InStr(IE.document.getElementById("body").innerHTML, "Here is my code") > 0 Then

Upvotes: 3

Related Questions