VBA IE 自动化 - 读取 iFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16699267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:40:10  来源:igfitidea点击:

VBA IE Automation - Read iFrame

internet-explorervbaiframeautomation

提问by Aaron Contreras

Our business uses a browser-based program for operations. I'm automating a solution to navigate through this site, and retrieve some data at the end.

我们的业务使用基于浏览器的程序进行运营。我正在自动化一个解决方案来浏览这个网站,并在最后检索一些数据。

The site itself uses regular frames very heavily. However, at the very end of my process, it populates my data not into a frame, but an iFrame. There's also very extensive javascript throughout the site, making things muddy.

该站点本身非常大量地使用常规框架。但是,在我的过程的最后,它不是将我的数据填充到框架中,而是填充到 iFrame 中。整个网站还有非常广泛的javascript,使事情变得混乱。

Grabbing the iFrame's src URL and opening in a new browser errors out the page (i.e. the page displays error text instead of the content).

获取 iFrame 的 src URL 并在新浏览器中打开错误页面(即页面显示错误文本而不是内容)。

My question:

我的问题:

How can I grab the text out of an iFrame through VBA?

如何通过 VBA 从 iFrame 中抓取文本?

What I've tried so far(feel free to skip):

到目前为止我尝试过的(随意跳过)

Target a specific iFrame in a specific frame, and grab innerHTML

针对特定框架中的特定 iFrame,并抓取 innerHTML

With ie.document.frames(myFrameNum).document.getElementsByTagName("iframe")(1).document.body
stringResult = .innerHTML

Target a specific iFrame by ID in a specific frame, and grab innerHTML

通过特定框架中的 ID 定位特定的 iFrame,并抓取innerHTML

Dim iFrm As HTMLIFrame
Set iFrm = ie.document.frames(myFrameNum).document.getElementByID("iFrameID")
Debug.Print iFrm.document.body.innerText

Find any instances of iFrames, and grab them(No results - perhaps because the iframe is embeded in a frame?)

找到 iFrame 的任何实例,并抓取它们(没有结果 - 可能是因为 iframe 嵌入在一个框架中?)

Dim iFrm As HTMLIFrame
Dim doc As HTMLDocument
 For iterator = 0 To ie.document.all.Length - 1
  If TypeName(ie.document.all(iterator)) = "HTMLIFrame" Then
   Set iFrm = ie.document.all(iterator)
   Set doc = iFrm.document
   Debug.Print & doc.body.outerHTML
  End If
Next

回答by abhinov

I faced the same issue and I got the solution using the following line of script..

我遇到了同样的问题,我使用以下脚本行得到了解决方案。

IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementsbyTagName("body")(0).innertext

IE.Document.getElementsbyTagName("iframe")(0)。contentDocument.getElementsbyTagName("body")(0).innertext

回答by Rajesh

Try this...

尝试这个...

Dim elemCollection As IHTMLElementCollection

Set elemCollection = objDoc.frames("iFrameID").document.all
Debug.Print elemCollection.Item("pagemenuli-adv").innerText

回答by Sukesh

I faced a similar problem, finally solved it using:

我遇到了类似的问题,最终使用以下方法解决了它:

ObjIE.document.frames(0).document.forms(0).innerText

ObjIE.document.frames(0).document.forms(0).innerText

Note: The text which I was in need was present in form which is located in a iframe.

注意:我需要的文本以位于 iframe 中的形式存在。

Am new to VBA, can some body explain what exactly 0 in Frames(0)/forms (0) is?

我是 VBA 的新手,有人可以解释一下 Frames(0)/forms(0) 中的 0 到底是什么吗?

If it something like Frame index or frame number(As of my assumption) please let me know how can we find frame index(in any HTML)?

如果它类似于帧索引或帧编号(根据我的假设),请告诉我我们如何找到帧索引(在任何 HTML 中)?

回答by Micha? Dziok

Try to use:

尝试使用:

`Dim iFrm As HTMLIFrame
Set iFrm = ie.document.frames(myFrameNum).document.getElementByID("iFrameID")
Debug.Print iFrm.contentDocument.body.innerHTML`