你能告诉我为什么 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>
用于后处理以通过 .innerText 或 .innerHTML 读取字符串 2083236893
在 GM 或 Fiddle 中工作的任何想法或任何工作脚本演示?