有许多在线资源说明了在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秒等待。我真的希望通过添加可靠逻辑来确定帧加载进度来提高此过程的性能。
我的问题是 - 有没有人知道一种聪明的方法来确定一个自治框架是否已经完成加载?
谢谢,
〜Z
' 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
答案 0 :(得分:0)
正如@JimmyPena所说。如果我们能看到网址,那么帮助会更容易。
如果我们不能,希望这个概述可以让你朝着正确的方向前进:
希望这有帮助!
答案 1 :(得分:0)
我使用循环选项来检查字段值,直到它填充为
执行IE.Document.getElementById(“USERID”)。值&lt;&gt; “TEST3” IE.Document.getElementById(“USERID”)。Value =“test3” 环
答案 2 :(得分:0)
这是一个非常古老的话题,但是我认为我会发表我的发现,因为我来这里是为了寻找答案...
在本地窗口中,我可以看到IE App本身的“ 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脚本...