我有15,000个产品,我需要知道它们是X,Y还是Z.下面的代码在亚马逊上检查产品是否为XYZ类型。使用VBA检查页面上是否存在元素
上帝帮助我,它实际上工作。唯一的例外是当它搜索亚马逊不再销售的产品时。那时,我正在寻找该元素的ID中包含的产品说明我正在搜索的页面上不存在,和在线
text = document.getelementbyID("result_0").innertext
代码中断与错误“对象变量或With块变量未设置“。
如何在继续执行其余代码之前检查元素是否存在?
谢谢!
山姆
Sub LetsAutomateIE()
Dim barcode As String
Dim rowe As Integer
Dim document As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
Dim Element As HTMLDivElement
Dim text As String
Dim pos As Integer
rowe = 2
While Not IsEmpty(Cells(rowe, 2))
barcode = Cells(rowe, "B").Value
With ie
.Visible = False
.navigate2 "https://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-
alias%3Daps&field-keywords=" & barcode
Do Until ie.readyState = 4
Loop
End With
Set document = ie.document
text = document.getElementById("result_0").innerText
If InStr(text, "X") Or InStr(text, "Y") Or InStr(text,
"Z") <> 0 Then pos = 1
If pos <> 0 Then Cells(rowe, 4) = "Y" Else Cells(rowe, 4) = "N"
rowe = rowe + 1
Wend
Set ie = Nothing
End Sub
使用错误捕获 –
尝试检查,看看只是返回的元素是一个对象。类似于'set Element = document.getelementbyID(“result_0”)'例如不要返回InnerText属性,那么'if isObject(Element)then'检查它是否作为一个对象返回。 –
谢谢瑞恩!我应该如何在设置之前调暗元素? – Sam