gpt4 book ai didi

Excel VBA : Working with iFrame via IE Automation

转载 作者:行者123 更新时间:2023-12-04 22:25:39 31 4
gpt4 key购买 nike

我有一个项目,我正在尝试通过 Excel 的 VBA 自动化网站的行为。到目前为止,我知道如何从 VBA 初始化 Web 浏览器、导航到网站以及执行简单的任务,例如使用 getElementById 单击项目。功能和click方法。但是,我想知道如何处理 iframe 内的嵌入式对象。

例如,这里通过 HTML 源代码概述了树结构的外观。当然,还有更多标签,但至少您可以了解我正在尝试做什么。

<html> 
<body>
<div>
<iframe class="abs0 hw100" scrolling="no" allowtransparency="true" id="Ifrm1568521068980" src="xxxxx" title="mailboxes - Microsoft Exchange" ldhdlr="1" cf="t" frameborder="0"> <<< ----- This is where I am stuck
<div>
<tbody>
<tr>
etc.....
etc.....
etc.....
</tr>
<tbody>
<div>
----------- close tags


我想对我来说最大的问题是学习如何操作包含在 iframe 中的嵌入式对象,因为所有这些对我来说仍然是新的,而且我不是用 VBA 编写程序的专家。任何朝着正确方向的帮助或指导都会对我有很大帮助。另外,如果您需要更多信息或澄清,请告诉我。

这是我到目前为止编写的代码:
Option Explicit

'Added: Microsoft HTML Object Library
'Added: Microsoft Internet Controls

'Added: Global Variable
Dim URL As String
Dim iE As InternetExplorer

Sub Main()

'Declaring local variables
Dim objCollection As Object
Dim objElement As Object
Dim counterClock As Long

Dim iframeDoc As MSHTML.HTMLDocument 'this variable is from stackoverflow (https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba)

'Predefining URL
URL = ""

'Created InternetExplorer Object
Set iE = CreateObject("InternetExplorer.Application")

'Launching InternetExplorer and navigating to the URL.
With iE
.Visible = True
.Navigate URL

'Waiting for the site to load.
loadingSite
End With

'Navigating to the page I need help with that contains the iFrame structure.
iE.Document.getElementById("Menu_UsersGroups").Click

'Waiting for the site to load.
loadingSite

'Set iframeDoc = iE.frames("iframename").Document '<<-- this is where the error message happens: 438 - object doesn't support this property or method.
'The iFrame of the page does not have a name. Instead "Ifrm1" is the ID of the iFrame.

End Sub

'Created a function that will be used frequently.

Function loadingSite()

Application.StatusBar = URL & " is loading. Please wait..."
Do While iE.Busy = True Or iE.ReadyState <> 4: Debug.Print "loading": Loop
Application.StatusBar = URL & " Loaded."

End Function

请注意:我的 VBA 编程知识处于入门级。所以,如果我第一次不理解你的答案,请多多包涵。另外,任何关于这个主题的漂亮文档或视频也会对我有很大帮助。无论哪种方式,我都非常坚定地学习这门语言,因为它对我来说变得非常有趣和有趣,尤其是当我可以让一个程序完全按照它的设计目的进行时。 :)

最佳答案

您尝试使用以下代码从 iframe 中获取元素:

IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click

示例代码如下:

索引页:
<input id="txtinput" type="text" /><br />
<input id="btnSayHello" type="button" value="Say Hello" onclick="document.getElementById('result').innerText = 'Hello ' + document.getElementById('txtinput').value" /><br />
<div id="result"></div><br />
<iframe width="500px" height="300px" src="vbaiframecontent.html">

</iframe>

vbaframeContent.html
<input id="txtcontentinput" type="text" /><br />
<input id="btncontentSayHello" type="button" value="Say Hello" onclick="document.getElementById('content_result').innerText = 'Hello ' + document.getElementById('txtcontentinput').value" /><br />
<div id="content_result"></div>

VBA脚本如下:
Sub extractTablesData1()
'we define the essential variables

Dim IE As Object, Data As Object
Dim ticket As String


Set IE = CreateObject("InternetExplorer.Application")

With IE
.Visible = True
.navigate ("<your website url>")

While IE.ReadyState <> 4
DoEvents
Wend

Set Data = IE.Document.getElementsbyTagName("input")

'Navigating to the page I need help with that contains the iFrame structure.
IE.Document.getElementbyId("txtinput").Value = "AAA"
IE.Document.getElementbyId("btnSayHello").Click


'Waiting for the site to load.
'loadingSite

IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click



End With
Set IE = Nothing
End Sub

运行脚本后,结果如下:

enter image description here

关于Excel VBA : Working with iFrame via IE Automation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57941535/

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