XPath错误自返回结果以来,文档已发生变异

时间:2018-01-12 23:12:37

标签: javascript html dom xpath

我尝试在网页上使用xpath,但收到错误Uncaught DOMException: Failed to execute 'iterateNext' on 'XPathResult': The document has mutated since the result was returned.

xpath = document.evalutate('//a', document)
xpath.iterateNext()

出了什么问题?

2 个答案:

答案 0 :(得分:3)

问题是页面在生成XPathResult对象和访问其中的对象的时间之间发生了变异。

而是使用快照

xpath = document.evaluate('//td/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
xpath.snapshotItem(0)
xpath.snapshotItem(1)

答案 1 :(得分:0)

如果您的建议是只读的,则可以克隆上下文节点:

xpath = document.evaluate('//a', document.cloneNode(true))
xpath.iterateNext()