【问题标题】:How Do I Test If Webpage Contains Certain Text如何测试网页是否包含某些文本
【发布时间】:2018-03-02 18:09:11
【问题描述】:

我正在尝试检测网页是否包含特定文本。例如,我想查看this web page 是否包含以下短语:“Here is my code”

我无法让它找到满足“如果那么”的条件。这是我正在尝试的:

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,但找不到这个确切的问题。

提前致谢!

【问题讨论】:

  • 试过document.body.innerText ?

标签: vba


【解决方案1】:

用途:

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

【讨论】:

  • 您好,如果这个答案让您走上了正确的道路,请勾选它,或者如果是其他内容,请随时将其添加到未来用户的答案中!
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 2018-06-17
  • 2018-05-24
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2018-03-25
  • 2013-12-13
相关资源
最近更新 更多