使用 VBScript 从 html 中的特定节点提取值
extracting value from specific node in html using VBScript
有没有办法在 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 函数,我仍然无法正确地使用 returns 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);
此 returns 特定于 DomXpath 的对象。在这种情况下,“//div[@class='division_product_tab']//img”是 img 对象。但这只适用于开发工具。
希望有解决办法
你可以试试 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 第一个节点,.querySelectorAll()
returns 一组节点。