【发布时间】:2017-12-27 04:01:51
【问题描述】:
你能告诉我为什么 XPath exaluate 给出了
snapshotLength = 1 for /html/body
和 0 代表
/html/body/div[3]/div[3]/div/div[6]/div[2]/div/div/div
较长的 XPath 是真实的,由 Firepath 为该页面上的对象计算得出
http://srv1.yogh.io/#mine:height:0
我在 Greasemonkey 中运行以下脚本。
// ==UserScript==
// @name xpath greasemonkey
// @namespace bbb
// @description f
// @match http://srv1.yogh.io/#mine:height:0
// @version 1
// @grant none
// ==/UserScript==
var allLinks, thisLink;
console.log("dfsdsdsdfg");
allLinks = document.evaluate(
"/html/body/div",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
// for (var i = 0; i < allLinks.snapshotLength; i++) {
// thisLink = allLinks.snapshotItem(i);
// console.log("found");
// }
console.log(allLinks.snapshotLength)
// /html/body/div[3]/div[3]/div/div[6]/div[2]/div/div/div
// ---
// UPDATED userscript
// ==UserScript==
// @name xpath greasemonkey2
// @namespace bbb
// @description f
// @match http://srv1.yogh.io/#mine:height:0
// @version 1
// @grant none
// ==/UserScript==
window.setTimeout(function() {
var allLinks, thisLink;
console.log("dfsdsdsdfg");
allLinks = document.evaluate(
"/html/body/div[3]/div[3]/div/div[6]/div[2]/div/div/div",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
if(allLinks.snapshotLength) {
console.log(allLinks.snapshotLength);
console.log(allLinks.snapshotItem(0).innerHTML)
} else {
console.log("Bother...");
console.log(allLinks.snapshotLength)
}
}, 15000);
function myFunction() {
if (window.top != window.self) {
alert("This window is not the topmost window! Am I in a frame?");
document.getElementById("demo").innerHTML = "This window is not the topmost window! Am I in a frame?";
} else {
alert("This window is the topmost window! Am I in a frame?");
document.getElementById("demo").innerHTML = "This window is the topmost window!";
}
}
var input=document.createElement("input");
input.type="button";
input.value="GreaseMonkey Button";
input.onclick = myFunction;
document.body.appendChild(input);
我想要代码在网页上找到 XPathed DIV 对象 突出显示它并通过 console.log 返回:
<div class="gwt-Label">2083236893</div>
用于后处理以通过以下方式读取字符串 2083236893 .innerText 或 .innerHTML
有任何想法或任何可在 GM 或 Fiddle 中工作的脚本演示吗?
【问题讨论】:
-
顺便说一句,我从stackoverflow.com/questions/762844/xpath-greasemonkey 继承了上面的示例,我在zvon.org/xxl/XPathTutorial/General/examples.html 学习了很棒的 XPath 教程示例,但是没有工具可以将网页变成类似的树节点图
-
什么是"XPath exaluate"?您的意思是“XPath 评估” 吗?或者可能是 “XPath 评估”,考虑到它所在的上下文?
标签: xpath greasemonkey document.evaluate