Excel VBA:通过IE自动化使用iFrame [英] Excel VBA: Working with iFrame via IE Automation

查看:119
本文介绍了Excel VBA:通过IE自动化使用iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project that I am working on where I am trying to automate a site's behavior via Excel's VBA. So far, I know how to initialize a web browser from VBA, navigate to a website, and perform a simple task such as clicking on an item using the getElementById function and click method. However, I wanted to know how can I go about working with an embedded object(s) that is inside of an iframe.

例如,这是通过HTML源代码对树结构的外观的概述.当然,还有很多标签,但是至少您可以了解我正在尝试执行的操作.

For example, here is an overview of what the tree structure looks like via HTML source code. Of course, there are a lot more tags, but at least you can get an idea of what it is that I am trying to do.

<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编写程序的专家.任何在正确方向上的帮助或指导都会对我有很大帮助.另外,如果您需要更多信息或说明,请告诉我.

I guess the biggest problem for me is to learn how to manipulate an embedded object(s) that is enclosed inside of an iframe because all of this is still new to me and I am not an expert in writing programs in VBA. Any help or guidance in the right direction will help me out a lot. Also, if you need more information or clarification, please let me know.

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

Here the code that I have written so far:

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编程的知识是入门级的.因此,如果我第一次不理解您的回答,请多多包涵.另外,有关此主题的任何精美文档或视频也将对我有很大帮助.无论哪种方式,我都决心学习这种语言,因为它对我来说变得非常有趣,特别是当我可以让一个程序完全按照其设计的目的进行操作时.:)

Please note: My knowledge of programming in VBA is on an entry-level. So, please bear with me if I don't understand your answer the first time around. Plus, any nifty documentation or videos about this topic will help me a lot as well. Either way, I'm very determined to learn this language as it is becoming very fun and interesting to me especially when I can get a program to do exactly what it was designed to do. :)

推荐答案

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

You try to use the following code to get elements from the 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

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

运行脚本后,结果如下:

After running the script, the result as below:

这篇关于Excel VBA:通过IE自动化使用iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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