0

After some days of research, I've found that other tools outside Excel/VBA use XHTML to be able to use Xpath.

Selenium is not an option since for security reasons is not allowed to install programs (.exe), then I was wondering if somebody knows if there is a microsoft library or how to convert from the HTML response in code below in line htmlDoc.body.innerHTML = .responseText to XHTML to be able to use Xpath expressions with SelectNodes() like in line Set elements = xhtmlDoc.SelectNodes("//body/div/div/div]")

Sub Html2Xhtml()
    Dim htmlDoc As New HTMLDocument
    Dim elements As MSXML2.IXMLDOMNodeList
    Dim url as String

    url = "https://www.example.com"

    With New ServerXMLHTTP60
        .Open "Get", url, False
        .send
        htmlDoc.body.innerHTML = .responseText
    End With
    
    xhtmlDoc = SomeFunction(htmlDoc.body.innerHTML)
    
    Set elements = xhtmlDoc.SelectNodes("//body/div/div/div]")
    
End Sub
4
  • Maybe conversion from the html document to xhtml is not needed if you use queryselector or queryselectorall on the html document directly: htmldoc.queryselectorall("body > div > div > div") Commented May 22, 2024 at 21:08
  • @ShahramAlemzadeh thanks for answer, then thing is I need to search by text content and querySelectorAll() doesn´t allow me do that. And not sure why, Excel crashes if I try to debug opening the varuable that stores the output os querySelectorAll Commented May 22, 2024 at 21:17
  • @Fravadona, thanks for links share, but it seems is not exactly what I'm trying to do Commented May 22, 2024 at 21:17
  • 1
    @RasecMalkic, assign the result of the queryselector to a variable of type ihtmldomchildrencollection and loop thorought it as an array. Something like this : dim elements as ihtmldomchildrencollection : set elements= htmldoc.queryselectorall("body > div > div > div") : dim i as integer : for i=0 to elements.length-1 : process elements(i) here : next i Commented May 22, 2024 at 21:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.