i am testing a code that checks if the item is out of stock on a given page. if the item is out of stock, the page contains a class:
>Out of stock
if not then the product is in stock. I have 2 links, one of which is out of stock, and the other is in stock and i have used query selectorAll to check ".stock out-of-stock" class exists and return false if it does not. My code returns True in both cases however.
Sub test()
Dim txt1 As String
Dim txt2 As String
txt1 = "https://www.cigarsofcuba.co.uk/shop/cuban-cigars/bolivar-cigars/bolivar-belgravia-uk-regional-2015-cigars-box-of-10/"
txt2 = "https://www.cigarsofcuba.co.uk/shop/cuban-cigars/bolivar-cigars/bolivar-belicosos-finos-cigars-box-of-25/"
a = getstock(txt1)
Debug.Print a
a=getstock (txt2)
Debug.Print a
End Sub
Function getstock(url As String) As Boolean
Set xhr = New MSXML2.XMLHTTP60
Set html = New MSHTML.HTMLDocument
With xhr
.Open "GET", url, False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
html.body.innerHTML = .responseText
End With
Dim ohtml As Object
Set ohtml = html.querySelectorAll(".stock out-of-stock")
If Not ohtml Is Nothing Then
getstock = True
End If
End Function
Will appreciate a guidance as to why it does not work. Thank you!
CodePudding user response:
The selector for an element with both classes should be:
Set ohtml = HTML.querySelectorAll(".stock.out-of-stock")
You need to determine the existence of the classes by looking at the length, i.e. ohtml.length
is 0
if the selector does not exist.
Set ohtml = HTML.querySelectorAll(".stock.out-of-stock")
getstock = ohtml.Length = 0