Skip to content

Instantly share code, notes, and snippets.

@seveniu
Last active August 29, 2015 14:05
Show Gist options
  • Save seveniu/9d631f64b22d2a91131c to your computer and use it in GitHub Desktop.
Save seveniu/9d631f64b22d2a91131c to your computer and use it in GitHub Desktop.
xpath 获取 frameset 内的元素
function getElementsByXpath(xpath,document) {
var nodes = [];
var iterator = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );
try {
var thisNode = iterator.iterateNext();
while (thisNode) {
nodes.push(thisNode);
thisNode = iterator.iterateNext();
}
}
catch (e) {
console.log( 'Error: Document tree modified during iteration ' + e );
}
console.log(nodes);
return nodes;
}
var doc2 = getElementsByXpath("//frameset/frame[1]",document)[0].contentDocument;
var list = getElementsByXpath("//a",doc2);
var urls = "";
for(var i = 0 ;i < list.length;i++) {
var a = list[i];
urls += a.href;
urls += "\n";
}
console.log(urls);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment