【发布时间】: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