Search code examples
javascriptinternet-explorervbscriptdomxpath

extracting value from specific node in html using VBScript


Is there any way to extract DomXPath of specific html elements using Vbscript in Internet Explorer???

 Sub WaitForLoad 'Sub to wait for browser to load
 Do While IE.Busy
   WScript.Sleep 10
 Loop   
End Sub

Dim IE
Dim example1
Dim example2

    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True 
    IE.navigate "http://www.lotteimall.com/goods/viewGoodsDetail.lotte?goods_no=12570568"
    WaitForLoad 

Set objRoot = ie.document

MsgBox objRoot.getElementById("win10").name


Dim vXPath : vXPath = "//*[@id='win10']"

Set example1 = objRoot.selectSingleNode(vXPath) 'this works for xml
Set example2 = objRoot.getElemetByXPath(vXPath) 'this works in javascript

'none of these works in IE html

there is no DomXPath function like 'getElemetByXPath' in IE document object, I couldn't still proper method that returns DomXPath.

As for the javascript,

    function XPath(elm) {
           for (segs = []; elm && elm.nodeType == 1; elm = elm.parentNode) {
             if (elm.hasAttribute('id')) {
               segs.unshift('id("' + elm.getAttribute('id') + '")')
               return segs.join('/')
             }
             else if (elm.hasAttribute('class'))
               segs.unshift(elm.localName.toLowerCase() + '[@class="' + elm.getAttribute('class') + '"]')
             else {
               for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling)
                 if (sib.localName == elm.localName) i++
               segs.unshift(elm.localName.toLowerCase() + '[' + i + ']')
             }
           }
           return segs.length ? '/' + segs.join('/') : null
    }
    var result = windows.document.evaluate("//div[@class='division_product_tab']//img", document, null, XPathResult.ANY_TYPE, null);

    var node, nodes = []
    while (node = result.iterateNext())
      nodes.push(XPath(node));

    console.log(nodes);

This returns object-specific of DomXpath. In this case, "//div[@class='division_product_tab']//img" which is img object. but this only work in Dev Tool.

I hope there might be solution for this


Solution

  • You may try to use CSS Selectors:

    Set oIE = CreateObject("InternetExplorer.Application")
    With oIE
        .Visible = True
        .Navigate "http://www.lotteimall.com/goods/viewGoodsDetail.lotte?goods_no=12570568"
        Do While .Busy Or .ReadyState <> 4
            WScript.Sleep 10
        Loop
        Do While .Document.readyState <> "complete"
            WScript.Sleep 10
        Loop
        Set oNode = .Document.querySelector("div.division_product_tab img")
        MsgBox oNode.src
    End With
    

    .querySelector() returns a first node, and .querySelectorAll() returns a collection of nodes.