有许多联机资源演示了如何在VBA Excel中使用Microsoft Internet Explorer控件来执行基本的IE自动化任务。当网页有一个基本结构时,这些方法就会起作用。但是,当网页包含多个框架时,它们可能很难处理。
我需要确定网页中的单个框架是否已完全加载。例如,这段VBA Excel代码打开IE,加载网页,遍历Excel工作表,将数据放入网页字段,执行搜索,然后将IE结果数据返回给Excel (很抱歉,我遗漏了站点地址)。
目标网页包含两个框架:
1)用于输入搜索值并执行搜索的searchbar.asp框架
2)显示搜索结果的searchresults.asp框架
在这个结构中,搜索栏是静态的,而搜索结果根据输入条件而变化。因为网页是以这种方式构建的,所以不能使用IEApp.ReadyState和IEApp.Busy来确定IEfr1框架加载完成,因为这些属性在初始search.asp加载之后不会更改。因此,我使用较大的静态等待时间,以避免在互联网流量波动时出现运行时错误。这段代码确实可以工作,但速度很慢。请注意cmdGO语句之后的10秒等待。我想通过添加可靠的逻辑来确定帧加载进度来提高性能。
如何确定自治帧是否已完成加载?
' NOTE: you must add a VBA project reference to "Internet Explorer Controls"
' in order for this code to work
Dim IEapp As Object
Dim IEfr0 As Object
Dim IEfr1 As Object
' Set new IE instance
Set IEapp = New InternetExplorer
' With IE object
With IEapp
' Make visible on desktop
.Visible = True
' Load target webpage
.Navigate "http://www.MyTargetWebpage.com/search.asp"
' Loop until IE finishes loading
While .ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
End With
' Set the searchbar.asp frame0
Set IEfr0 = IEapp.Document.frames(0).Document
' For each row in my worksheet
For i = 1 To 9999
' Input search values into IEfr0 (frame0)
IEfr0.getElementById("SearchVal1").Value = Cells(i, 5)
IEfr0.getElementById("SearchVal2").Value = Cells(i, 6)
' Execute search
IEfr0.all("cmdGo").Click
' Wait a fixed 10sec
Application.Wait (Now() + TimeValue("00:00:10"))
' Set the searchresults.asp frame1
Set IEfr1 = IEapp.Document.frames(1).Document
' Retrieve webpage results data
Cells(i, 7) = Trim(IEfr1.all.Item(26).innerText)
Cells(i, 8) = Trim(IEfr1.all.Item(35).innerText)
Next发布于 2012-07-11 05:09:00
正如@JimmyPena所说。如果我们能看到URL,帮助起来就容易多了。
如果我们做不到,希望这篇概述能帮你找到正确的方向:
document等待页面加载(从IE对象中IEApp.ReadyState和
希望这能有所帮助!
发布于 2014-10-17 20:21:15
我使用循环选项检查字段值,直到它像下面这样填充
Do While IE.Document.getElementById("USERID").Value <> "test3“IE.Document.getElementById("USERID").Value = "test3”循环
发布于 2019-07-24 07:25:17
这是一个非常老的帖子,但我想我应该发布我的发现,因为我来这里是为了寻找答案……
查看本地变量窗口,我可以看到对于IE应用程序本身,"readystate“变量只有"READYSTATE_COMPLETE”。但是对于iframe,它是小写的"complete“
因此,我通过在我正在处理的帧的.readystate上使用debug.print循环来探索这一点。
Dim IE As Object
Dim doc As MSHTML.HTMLDocument
Set doc = IE.Document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = doc.Frames("TheFrameIwasWaitingFor").Document
' then, after I had filled in the form and fired the submit event,
Debug.Print iframeDoc.readyState
Do Until iframeDoc.readyState = "complete"
Debug.Print iframeDoc.readyState
DoEvents
Loop因此,这将在“即时”窗口中显示一行接一行的“加载”,最终显示“完成”并结束循环。当然,可以对其进行删节以删除debug.prints。
另一件事:
debug.print iframeDoc.readystate ' is the same as...
debug.print doc.frames("TheFrameIwasWaitingFor").Document.readystate
' however, you cant use...
IE.Document.frames("TheFrameIwasWaitingFor").Document.readystate ' for some reason...如果所有这些都是常识,请原谅。我几天前才真正学会了VBA脚本...
https://stackoverflow.com/questions/10954496
复制相似问题