如何使用VBA excel等待IE9帧加载? [英] How does one wait for an IE9 frame to load using VBA excel?

查看:173
本文介绍了如何使用VBA excel等待IE9帧加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多在线资源说明了在VBA Excel中使用Microsoft Internet Explorer控件来执行基本的IE自动化任务。当有问题的网页有一个基本的构造时,这些工作很棒。但是,当网页包含多个框架时,可能难以使用。目前,我有问题确定网页中的单个框架是否已完全加载。例如...这个VBA Excel代码打开IE,加载一个网页,循环一个excel表格将数据放入网页字段,执行搜索,然后将IE结果数据返回给excel(我的歉意,省略了站点地址)。这里的目标网页包含两个框架:

There are many online resources that illustrate using Microsoft Internet Explorer Controls within VBA Excel to perform basic IE automation tasks. These work great when the webpage in question has a basic construct. However, when webpages contain multiple frames they can be difficult to work with. Currently I am having issues determining if an individual frame within a webpage has completely loaded. For example... This VBA Excel code opens IE, loads a webpage, loops thur an excel sheet placing data into the webpage fields, executes search, and then returns the IE results data to excel (my apologies for omitting the site address). The rub here is that the target webpage contains two frames:

1)用于搜索值输入和执行搜索的searchbar.asp框架

1) The searchbar.asp frame for search value input and executing search

2)用于显示搜索结果的searchresults.asp框架

2) The searchresults.asp frame for displaying search results

在此构造中,搜索栏是静态的,而搜索结果根据输入条件而变化。因为网页是以这种方式构建的,所以IEApp.ReadyState和IEApp.Busy不能用于确定IEfr1框架加载完成,因为这些属性在初始的search.asp加载之后不会改变。因此,我使用大量的静态等待时间来避免网络流量排队时的运行时错误。这段代码确实有效,但是它是可笑的慢...注意在cmdGO语句后等待10秒。我真的希望通过添加坚实的逻辑来确定框架加载进度来提高这个过程的性能。

In this construct the search bar is static, while the search results change according to input criteria. Because the webpage is built in this manner, the IEApp.ReadyState and IEApp.Busy cannot be used to determine IEfr1 frame load completion, as these properties do not change after the initial search.asp load. Therefore, I use a large static wait time to avoid runtime errors as internet traffic flucuates. This code does work, but it is ridiculously slow... note the 10 second wait after the cmdGO statement. I would really like to improve the performance of this process by adding solid logic to determine the frame load progress.

我的问题是 - 有谁知道一个聪明的方式确定自动框架是否已经完成加载?

My question is this - Does anyone know of a clever way to determine if an autonomous frame has finished loading?

谢谢,

〜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


推荐答案

我使用循环选项来检查d值,直到它填充这样

I used loop option to check the field value until its populated like this

尽管IE.Document.getElementById(USERID)。值> test3
IE.Document。 getElementById(USERID)。Value =test3
循环

Do While IE.Document.getElementById("USERID").Value <> "test3" IE.Document.getElementById("USERID").Value = "test3" Loop

这篇关于如何使用VBA excel等待IE9帧加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆