Excel VBA HTML Nested QuerySelector
Consider this extract of an html page: Document
Solution 1:
nth-child(2)
is not supported in VBA and is indeed causing the error message. You can't use :nth-child()
or :nth-of-type()
. There is very little implemented in libraries available to you that deal with pseudo-classes. You can use first-child
interestingly. You will also find you are limited on which objects you can chain querySelector on.
Dim ele As Object, iText As String
Set ele = html.querySelector(".BoxBody > p > span:first-child > a[title='Next page']")
On Error Resume Next
iText = ele.href
On Error GoTo 0
If iText = vbNullString Then '<== This assumes that the href has a value otherwise use an On Error GoTo which will then handle the error and print "no href"
Debug.Print "No href"
Else
Debug.Print "href"
End If
EDIT: 29/5/21 As of some point in last month (?) it has become possible to use element.querySelector widely as well as the most of the standard pseudo-class selectors (at least for Windows 10, MSHTML.DLL 11.00.19041.985 (Date modified 12/5/21)
Post a Comment for "Excel VBA HTML Nested QuerySelector"