【发布时间】:2020-01-16 09:17:16
【问题描述】:
我有一个项目,我正在尝试通过 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 然后 .contentDocument.accessorMethodGoesHere... .... 如果您导航到 iframe 的 src,那么您只需使用没有 contentDocument 的正常访问语法,因为您已经在相关的 HTMLDocument 中。我认为这个问题需要更具体一点......我正在尝试做 x 并且 y 正在发生,但我想要 z。
-
如果网页嵌入了 iframe,则意味着您在浏览器中没有其他浏览器。我能想象的唯一方法是获取这个 iframe 的 URL 并导航到那个 URL。 iframe 是独立的对象。有一些安全原因也试图阻止大量交互。
标签: excel vba internet-explorer automation