1

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

Connor Lee
  • 69
  • 6
  • Did you try this code? `objRoot.getElemetByXPath(vXPath)` isn't going to work in anything, think you mean `getElementByXPath()`. Plus it is a helper function that makes use of [`document.evaluate()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_using_XPath_in_JavaScript#document.evaluate) it isn't part of vanilla JavaScript. – user692942 Oct 04 '18 at 19:59
  • yeap, it does not, I've been looking for the method like those, which are available in VBScript – Connor Lee Oct 05 '18 at 05:03
  • Possible duplicate of [How can I extract a value from an html page in vbscript - I tried MSXML2.DOMDocument](https://stackoverflow.com/questions/27442645/how-can-i-extract-a-value-from-an-html-page-in-vbscript-i-tried-msxml2-domdocu) – user692942 Oct 05 '18 at 05:40

1 Answers1

1

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.

omegastripes
  • 12,351
  • 4
  • 45
  • 96