Skip to content Skip to sidebar Skip to footer

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 AsObject, iText AsStringSet ele = html.querySelector(".BoxBody > p > span:first-child > a[title='Next page']")
   
OnErrorResumeNext
iText = ele.href
OnErrorGoTo0If 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"EndIf

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"