Last active
January 14, 2022 07:11
-
-
Save guozi/d26ca6692584d93bee43648742a751fa to your computer and use it in GitHub Desktop.
[Js根据xpath获取元素或者元素值]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _x(STR_XPATH) { | |
var xresult = document.evaluate(STR_XPATH, document, null, XPathResult.ANY_TYPE, null); | |
var xnodes = []; | |
var xres; | |
while (xres = xresult.iterateNext()) { | |
// 获取元素 | |
xnodes.push(xres); | |
// 获取指定元素的值 | |
//xnodes.push(xres.getAttribute('data-cgt-code')); | |
} | |
return xnodes; | |
} | |
// 使用 | |
console.log(_x('//ul[@id="newul"]/li')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _x() { | |
var xresult = document.evaluate('//ul[@id="newul"]/li', document, null, XPathResult.ANY_TYPE, null); | |
var xnodes = new Map(); | |
var xres; | |
while (xres = xresult.iterateNext()) { | |
xnodes.set(xres.getAttribute('data-cgt-code'),xres.getAttribute('title')); | |
} | |
console.log(xnodes); | |
array = Array.from(xnodes, ([name, value]) => ({ name, value })); | |
console.log(array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment