本文介绍了在VBA中使用getElementByClassName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此代码从页面获取产品名称页面代码为
I am using this code to get product name from a pagecode of page is
<div class="product-shop col-sm-7">
<div class="product-name">
<h1 >Claro Glass 1.5 L Rectangular Air Tight Food Container with Lid- Clear GMA0215A</h1>
</div>
我的vba代码是
Public Sub GetValueFromBrowser()
Dim ie As Object
Dim name As String
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1).Value = "RUNNING"
URL = Selection.Value
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = 0
.navigate URL
While .Busy Or .readyState <> 4
DoEvents
Wend
End With
Dim Doc As HTMLDocument
Set Doc = ie.document
ActiveCell.Offset(0, 1).Value = "ERROR"
name = Trim(Doc.getElementByClassName("product-name").innerText)
ActiveCell.Offset(0, 1).Value = name
ie.Quit
Loop
End Sub
我遇到的错误是
对象不支持此属性或方法
Object doesn't support this property or method
推荐答案
GetElementsByClassName方法
您在方法getElement s ByClassName的名称中缺少 s
.
GetElementsByClassName method
You are missing an s
in the name of the method getElementsByClassName.
- 更改此
name = Trim(Doc.getElementByClassName("product-name").innerText)
- 对此
name = Trim(Doc.getElementsByClassName("product-name")(0).innerText)
.用(0)
替换您要定位的项目.
- Change this
name = Trim(Doc.getElementByClassName("product-name").innerText)
- To this
name = Trim(Doc.getElementsByClassName("product-name")(0).innerText)
. Substitude the(0)
for the item you are targeting.
这篇关于在VBA中使用getElementByClassName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!