有什么方法可以在Internet Explorer中使用Vbscript提取特定html元素的DomXPath吗?
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
在IE文档对象中没有像'getElemetByXPath'这样的DomXPath函数,我仍然无法返回DomXPath的正确方法。
关于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);
这将返回特定于对象的DomXpath。在这种情况下,“ // div [@ class ='division_product_tab'] // img”是img对象。但这仅在开发工具中有效。
我希望对此有解决方案
答案 0 :(得分:1)
您可以尝试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()
返回第一个节点,.querySelectorAll()
返回节点的集合。