如何测试网页是否包含某些文本

问题描述 投票:1回答:1

我正在尝试检测网页是否有某些文字。例如,我想看看this web page是否包含以下短语:“这是我的代码”

我无法让它发现满足“If Then”条件。这是我正在尝试的:

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

我也尝试过:

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

 If FindText > 0 Then

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

但没有任何作用。我查看了Stack Overflow,但找不到这个确切的问题。

提前致谢!

vba
1个回答
1
投票

使用:

If InStr(IE.document.getElementById("body").innerHTML, "Here is my code") > 0 Then
© www.soinside.com 2019 - 2024. All rights reserved.