正确访问vba中的iFrame

时间:2015-07-13 12:21:12

标签: excel vba excel-vba excel-2010

我正在尝试从亚马逊获取产品描述字段。

例如:http://www.amazon.com/GMC-Denali-Black-22-5-Inch-Medium/dp/B00FNVBS5C/ref=sr_1_1?s=outdoor-recreation&ie=UTF8&qid=1436768082&sr=1-1&keywords=bicycle

这是我的代码:

Sub ScrapeProductDesc()

 Dim Ie As New InternetExplorer
 Dim WebURL
 Dim Docx As HTMLDocument
 Dim productDesc
 Dim RcdNum


Ie.Visible = False

For RcdNum = 2 To ThisWorkbook.Worksheets(1).Range("A65536").End(xlUp).Row

WebURL = ThisWorkbook.Worksheets(1).Range("A" & RcdNum)
 Ie.Navigate2 WebURL
 Do Until Ie.readyState = READYSTATE_COMPLETE
 DoEvents
 Loop
 Set Docx = Ie.document
 productTitle = Docx.getElementById("productTitle").innerText

 '####### Product Desc
 productDesc = Ie.document.Window.frames("product-description-iframe").contentWindow.document.getElementsByClassName("productDescriptionWrapper").innerText

 'print to workbook
 ThisWorkbook.Worksheets(1).Range("A" & RcdNum) = productDesc
 Next

End Sub

由于产品说明位于我正在执行的IFrame中:productDesc = Ie.document.Window.frames("product-description-iframe").contentWindow.document.getElementsByClassName("productDescriptionWrapper").innerText

但是,我收到此行的错误消息:Object does not support this property or method.

我想访问iFrame是不正确的。

有关如何正确访问iframe的任何建议吗?

1 个答案:

答案 0 :(得分:1)

尝试将变量声明为As MSHTML.HTMLWindow2并将其设置为Docx.frames(0)

Sub ScrapeProductDesc()

 Dim Ie As New InternetExplorer
 Dim WebURL
 Dim Docx As HTMLDocument
 Dim productDesc
 Dim RcdNum
 Dim prdt As MSHTML.HTMLWindow2



Ie.Visible = False

For RcdNum = 2 To ThisWorkbook.Worksheets(1).Range("A65536").End(xlUp).Row

WebURL = ThisWorkbook.Worksheets(1).Range("A" & RcdNum)
 Ie.Navigate2 WebURL
 Do Until Ie.ReadyState = READYSTATE_COMPLETE
 DoEvents
 Loop
 Set Docx = Ie.Document
 Set prdt = Docx.frames(0)
 productTitle = prdt.Document.getElementById("productTitle").innerText

 '####### Product Desc
 productDesc = prdt.Document.getElementsByClassName("productDescriptionWrapper")(0).innerText

 'print to workbook
 ThisWorkbook.Worksheets(1).Range("A" & RcdNum) = productDesc
 Next

End Sub