html - VBA wait for everything in a webpage to completely load
问题描述
I'm trying to pull some information from a website after navigating to it but I can't seem to wait until it completely loads. I've been trying to loop until the class at (0) contains text. Anyone know what I'm doing wrong?
Sub test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
Dim elements2 As IHTMLElementCollection
IE.Navigate "https://www.facebook.com/marketplace/item/955559644646354/"
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Dim x
x = 0
Do Until x = 1
Set elements2 = IE.document.getElementsByClassName("_3cgd")
If WorksheetFunction.IsText(elements2(0).innerText) = True Then
MsgBox ((elements2(0).innerText))
x = 1
Else
Application.Wait Now + #12:00:01 AM#
End If
Loop
End Sub
解决方案
尝试这样的事情(未经测试)
Dim t, elements2, txt
t = Timer
txt = ""
Do
Set elements2 = IE.document.getElementsByClassName("_3cgd")
If elements2.length > 0 Then
txt = elements2(0).innerText
If Len(txt) > 0 Then Exit Do
End If
If (Timer - t) > 10 Then Exit Do 'exit if too long waiting
Application.Wait Now + TimeSerial(0, 0, 1)
Loop
推荐阅读
- jquery - 滚动时单击事件后,活动菜单项不会更改他的类
- r - 将一个数据帧拆分为多个数据帧
- java - RMI 服务器端 java.net.MalformedURLException:未知协议:c
- c# - 使用 Lambda 表达式参数调用通用方法(以及仅在运行时知道的类型)
- regex - 正则表达式以未定义的顺序匹配给定数量的字符
- git - Bitbucket 拉取请求,其中源 repo 是 forkee,目标 repo 是 fork
- vb.net - 如何访问模块中的功能
- csom - 具有基于声明的身份验证的项目服务器内部 2016 CSOM 代码
- javascript - Vue.js,有条件地将项目添加到数组
- set - CPLEX OPL 返回具有条件的集合索引