使用VBA访问iframe中的对象

时间:2023-01-28 15:56:37

To the point:

要点:

I have successfully used VBA to do the following:

我已成功使用VBA执行以下操作:

  • Login to a website using getElementsByName

    使用getElementsByName登录网站

  • Select parameters for the report that will be generated (using getelementsby...)

    选择将生成的报告的参数(使用getelementsby ...)

  • generating the report after selecting parameters which renders the resulting dataset into an iframe on the same page
  • 选择参数后生成报告,这些参数将生成的数据集呈现在同一页面上的iframe中

Important to note - The website is client-side

需要注意的重要事项 - 该网站是客户端的

The above was the simple part, the difficult part is as below:

以上是简单的部分,困难的部分如下:

clicking on a gif image within the iframe that exports the dataset to a csv

单击iframe中的gif图像,将数据集导出到csv

I have tried the following:

我尝试过以下方法:

Dim idoc As HTMLDocument
Dim iframe As HTMLFrameElement
Dim iframe2 As HTMLDocument

Set idoc = objIE.document
Set iframe = idoc.all("iframename")
Set iframe2 = iframe.contentDocument

    Do Until InStr(1, objIE.document.all("iframename").contentDocument.innerHTML, "img.gif", vbTextCompare) = 0
        DoEvents
    Loop

To give some context to the logic above -

为上面的逻辑提供一些背景 -

  • I accessed the main frame
  • 我访问了主框架
  • i accessed the iframe by its name element
  • 我通过名称元素访问iframe
  • i accessed the content within the iframe
  • 我访问了iframe中的内容
  • I attempted to find the gif image that needs to be clicked to export to csv
  • 我试图找到需要点击导出到csv的gif图像

It is at this line that it trips up saying "Object doesn't support this property or method"

正是在这条线上它说“对象不支持这个属性或方法”

Also tried accessing the iframe gif by the a element and href attribute but this totally failed. I also tried grabbing the image from its source URL but all this does it take me to the page the image is from.

还尝试通过a元素和href属性访问iframe gif,但这完全失败了。我也尝试从其源URL抓取图像,但所有这一切都将我带到图像所在的页面。

note: the iframe does not have an ID and strangely the gif image does not have an "onclick" element/event

注意:iframe没有ID,奇怪的是gif图像没有“onclick”元素/事件

Final consideration - attempted scraping the iframe using R

最后的考虑 - 尝试使用R来抓取iframe

accessing the HTML node of the iframe was simple, however trying to access the attributes of the iframe and subsequently the nodes of the table proved unsuccessful. All it returned was "Character(0)"

访问iframe的HTML节点很简单,但是尝试访问iframe的属性,随后表的节点被证明是不成功的。它返回的只是“Character(0)”

library(rvest)
library(magrittr)

Blah <-read_html("web address redacted") %>%
  html_nodes("#iframe")%>%
  html_nodes("#img")%>%
  html_attr("#src")%>%
  #read_html()%>%
  head()
Blah

As soon as a i include read_html the following error returns on the script:

只要i包含read_html,脚本就会返回以下错误:

Error in if (grepl("<|>", x)) { : argument is of length zero

if(grepl(“<|>”,x)){:参数的长度为零时出错

I suspect this is referring to the Character(0)

我怀疑这是指字符(0)

Appreciate any guidance here!

感谢这里的任何指导!

Many Thanks,

非常感谢,

HTML

HTML

<div align="center"> 
    <table id="table1" style="border-collapse: collapse" width="700" cellspacing="0" cellpadding="0" border="0"> 
        <tbody>
            <tr>
                <td colspan="6"> &nbsp;</td>
            </tr> 
            <tr> 
                <td colspan="6"> 
                    <a href="href redacted">
                        <img src="img.gif" width="38" height="38" border="0" align="right">
                    </a>
                    <strong>x - </strong>
                </td>
            </tr> 
        </tbody>
    </table>
</div>

1 个解决方案

#1


5  

It is sometimes tricky with iframes. Based on html you provided I have created this example. Which works locally, but would it work for you as well?

iframe有时很棘手。基于你提供的html我创建了这个例子。哪个在本地工作,但它也适合你吗?

To get to the IFrame the frames collection can be used. Hope you know the name of the IFrame?

要进入IFrame,可以使用帧集合。希望你知道IFrame的名字?

Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = doc.frames("iframename").document

Then to go the the image we can use querySelector method e.g. like this:

然后去图像我们可以使用querySelector方法,例如喜欢这个:

Dim img As MSHTML.HTMLImg
Set img = iframeDoc.querySelector("div table[id='table1'] tbody tr td a[href^='https://*.com'] img")

The selector a[href^='https://*.com'] selects anchor which has an href attribute which starts with given text. The ^ denotes the beginning.

选择器a [href ^ ='https://*.com']选择具有以给定文本开头的href属性的锚。 ^表示开头。

Then when we have the image just a simple call to click on its parent which is the desired anchor. HTH

然后,当我们对图像进行简单的调用时,单击其父级即所需的锚点。 HTH


Complete example:

完整的例子:

Option Explicit

' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library

Sub Demo()

    Dim ie As SHDocVw.InternetExplorer
    Dim doc As MSHTML.HTMLDocument
    Dim url As String

    url = "file:///C:/Users/dusek/Documents/My Web Sites/mainpage.html"
    Set ie = New SHDocVw.InternetExplorer
    ie.Visible = True
    ie.navigate url

    While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
        DoEvents
    Wend

    Set doc = ie.document

    Dim iframeDoc As MSHTML.HTMLDocument
    Set iframeDoc = doc.frames("iframename").document
    If iframeDoc Is Nothing Then
        MsgBox "IFrame with name 'iframename' was not found."
        ie.Quit
        Exit Sub
    End If

    Dim img As MSHTML.HTMLImg
    Set img = iframeDoc.querySelector("div table[id='table1'] tbody tr td a[href^='https://*.com'] img")
    If img Is Nothing Then
        MsgBox "Image element within iframe was not found."
        ie.Quit
        Exit Sub
    Else
        img.parentElement.Click
    End If

    ie.Quit
End Sub

Main page HTML used

使用主页HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!-- saved from url=(0016)http://localhost -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>x -</title>
</head>

<body>
<iframe name="iframename" src="iframe1.html">
</iframe>
</body>

</html>

IFrame HTML used (saved as file iframe1.html)

使用IFrame HTML(保存为文件iframe1.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!-- saved from url=(0016)http://localhost -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 2</title>
</head>

<body>
<div align="center"> 
    <table id="table1" style="border-collapse: collapse" width="700" cellspacing="0" cellpadding="0" border="0"> 
        <tbody>
            <tr>
                <td colspan="6"> &nbsp;</td>
            </tr> 
            <tr> 
                <td colspan="6"> 
                    <a href="https://*.com/questions/44902558/accessing-object-in-iframe-using-vba">
                        <img src="img.gif" width="38" height="38" border="0" align="right">
                    </a>
                    <strong>x - </strong>
                </td>
            </tr> 
        </tbody>
    </table>
</div>

</body>

</html>

#1


5  

It is sometimes tricky with iframes. Based on html you provided I have created this example. Which works locally, but would it work for you as well?

iframe有时很棘手。基于你提供的html我创建了这个例子。哪个在本地工作,但它也适合你吗?

To get to the IFrame the frames collection can be used. Hope you know the name of the IFrame?

要进入IFrame,可以使用帧集合。希望你知道IFrame的名字?

Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = doc.frames("iframename").document

Then to go the the image we can use querySelector method e.g. like this:

然后去图像我们可以使用querySelector方法,例如喜欢这个:

Dim img As MSHTML.HTMLImg
Set img = iframeDoc.querySelector("div table[id='table1'] tbody tr td a[href^='https://*.com'] img")

The selector a[href^='https://*.com'] selects anchor which has an href attribute which starts with given text. The ^ denotes the beginning.

选择器a [href ^ ='https://*.com']选择具有以给定文本开头的href属性的锚。 ^表示开头。

Then when we have the image just a simple call to click on its parent which is the desired anchor. HTH

然后,当我们对图像进行简单的调用时,单击其父级即所需的锚点。 HTH


Complete example:

完整的例子:

Option Explicit

' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library

Sub Demo()

    Dim ie As SHDocVw.InternetExplorer
    Dim doc As MSHTML.HTMLDocument
    Dim url As String

    url = "file:///C:/Users/dusek/Documents/My Web Sites/mainpage.html"
    Set ie = New SHDocVw.InternetExplorer
    ie.Visible = True
    ie.navigate url

    While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
        DoEvents
    Wend

    Set doc = ie.document

    Dim iframeDoc As MSHTML.HTMLDocument
    Set iframeDoc = doc.frames("iframename").document
    If iframeDoc Is Nothing Then
        MsgBox "IFrame with name 'iframename' was not found."
        ie.Quit
        Exit Sub
    End If

    Dim img As MSHTML.HTMLImg
    Set img = iframeDoc.querySelector("div table[id='table1'] tbody tr td a[href^='https://*.com'] img")
    If img Is Nothing Then
        MsgBox "Image element within iframe was not found."
        ie.Quit
        Exit Sub
    Else
        img.parentElement.Click
    End If

    ie.Quit
End Sub

Main page HTML used

使用主页HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!-- saved from url=(0016)http://localhost -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>x -</title>
</head>

<body>
<iframe name="iframename" src="iframe1.html">
</iframe>
</body>

</html>

IFrame HTML used (saved as file iframe1.html)

使用IFrame HTML(保存为文件iframe1.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!-- saved from url=(0016)http://localhost -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 2</title>
</head>

<body>
<div align="center"> 
    <table id="table1" style="border-collapse: collapse" width="700" cellspacing="0" cellpadding="0" border="0"> 
        <tbody>
            <tr>
                <td colspan="6"> &nbsp;</td>
            </tr> 
            <tr> 
                <td colspan="6"> 
                    <a href="https://*.com/questions/44902558/accessing-object-in-iframe-using-vba">
                        <img src="img.gif" width="38" height="38" border="0" align="right">
                    </a>
                    <strong>x - </strong>
                </td>
            </tr> 
        </tbody>
    </table>
</div>

</body>

</html>