Last active
August 29, 2015 14:05
-
-
Save seveniu/9d631f64b22d2a91131c to your computer and use it in GitHub Desktop.
xpath 获取 frameset 内的元素
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 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