无法在< p>内获取文本使用VBA标记 [英] Cannot get the text inside a <p> tag using VBA
问题描述
我有以下网址
https://www.wavemotion.gr/shop/smartphone-accessories/easy-touch-wireless-wireless-fast-charge-mount
我正在尝试通过以下方法获取该产品的可用性
I am trying to get the availability of the product by using the following
For i = 2 To lastrow
mylink = wks.Cells(i, 2).Value
ie.Navigate mylink
While ie.Busy Or ie.ReadyState < 4: DoEvents: Wend
t = Timer
Do
DoEvents
On Error Resume Next
Set instock = ie.Document.querySelector(".stock.in-stock").innerText
If instock Is Nothing Then
Set availability = ie.Document.querySelector(".stock.out-of-stock").innerText
Else
Set availability = instock
End If
wks.Cells(i, "D") = availability
If Timer - t > MAX_WAIT_SEC Then Exit Do
On Error GoTo 0
Loop
Next i
但是我对
Set instock = ie.Document.querySelector(".stock.in-stock").innerText
我检查了查询
正在工作
我在这里做错了什么?没有任何ID只能定位类名
What I am doing wrong here? There is not any id to target only class name
<p class="stock in-stock">Διαθέσιμο</p>
推荐答案
所以,这里正在发生的事情是您正在尝试将 Set
字符串数据类型 innerText
设置为对象变量库存
.它返回 Nothing
的原因是因为您的 On Error Resume Next
语句禁止显示错误消息.如果将其取出并运行,则会得到 Type Mismatch
.您需要做的是将其拆分为一行,将对象分配给对象变量,然后一行读取所分配对象的 innerText
.
So, what's happening here is that you're trying to Set
string datatype innerText
to object variable instock
. The reason it's returning Nothing
is because your On Error Resume Next
statement is suppressing the error message. If you took that out and ran it, you would get a Type Mismatch
. What you'd need to do is split it into a line that assigns the object to the object variable and then a line that reads the innerText
of the assigned object.
Set instock = ie.Document.querySelector(".stock.in-stock")
If instock Is Nothing Then
Set availability = ie.Document.querySelector(".stock.out-of-stock")
Else
Set availability = instock
End If
wks.Cells(i, "D") = availability.innerText
这篇关于无法在< p>内获取文本使用VBA标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!