[有许多在线资源说明了如何使用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加载之后这些属性不会更改。因此,我使用大量的静态等待时间来避免由于Internet流量波动而导致的运行时错误。这段代码确实有效,但是速度很慢。请注意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
正如@JimmyPena所说。如果我们可以看到URL,则可以轻松得多。
如果我们做不到,希望此概述可以为您指明正确的方向:
希望这会有所帮助!
我使用循环选项来检查字段值,直到像这样填充为止
在IE.Document.getElementById(“ USERID”)。Value <>“ test3”IE.Document.getElementById(“ USERID”)。Value =“ test3”循环
这是一个非常古老的话题,但是我认为我会发表我的发现,因为我来这里是为了寻找答案...
[在本地窗口中,我可以看到IE应用程序本身的“ readystate”变量仅为“ READYSTATE_COMPLETE”。但对于iframe,它是小写的“完整”因此,我通过在正在处理的帧的.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脚本...