gpt4 book ai didi

excel - 如何使用 VBA Excel 等待 Internet Explorer 9 框架加载?

转载 作者:行者123 更新时间:2023-12-04 21:07:04 24 4
gpt4 key购买 nike

有许多在线资源说明如何在 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

最佳答案

正如@JimmyPena 所说。如果我们能看到 URL,那么提供帮助会容易得多。

如果我们不能,希望此概述可以为您指明正确的方向:

  • 等待页面加载(IEApp.ReadyState 和 IEApp.Busy)
  • 从 IE 对象中获取文档对象。 (完成)
  • 循环直到文档对象不是什么都没有。
  • 从文档对象中获取框架对象。
  • 循环直到框架对象不是什么都没有。

  • 希望这可以帮助!

    关于excel - 如何使用 VBA Excel 等待 Internet Explorer 9 框架加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10954496/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com