为什么 XPath exaluate 为 /html/body 提供 snapshotLength = 1,为 /html/body/div[3]/div[3]/div/div[6]/[ 提供 0 =14=][2]/div/div/div?
Why does XPath exaluate gives snapshotLength = 1 for /html/body and 0 for /html/body/div[3]/div[3]/div/div[6]/div[2]/div/div/div?
你能告诉我为什么 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 return 作为:
<div class="gwt-Label">2083236893</div>
用于后处理以通过以下方式读取字符串 2083236893
.innerText 或 .innerHTML
在 GM 或 Fiddle 中工作的任何想法或任何工作脚本演示?
您的 XPath 实际上是可以的,如果您直接在浏览器的控制台中输入它应该可以工作。问题在于区块链网页的内容是由脚本动态生成的,因此您要查找的节点在执行用户脚本时不存在。
您需要将代码从 运行ning 延迟到页面加载完成之后。 @run-at 用户脚本元数据指令应该可以实现此目的,但要么它在 GreaseMonkey v4 中被破坏,要么 Blockchain 脚本花费太长时间 运行 并且您需要将您的用户脚本延迟更长时间。无论哪种方式,将您的代码包装在 setTimeout()
中是实现该技巧的简单方法(检测其他脚本何时完成会更优雅):
// ==UserScript==
// @name xpath greasemonkey
// @namespace bbb
// @description f
// @match http://srv1.yogh.io/
// @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.snapshotItem(0).innerHTML);
} else {
console.log("Bother...");
}
}, 5000);
(请注意,我还必须更改您的 @match
指令以使 Greasemonkey 成为右侧页面上的 运行 脚本。
你能告诉我为什么 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 return 作为:
<div class="gwt-Label">2083236893</div>
用于后处理以通过以下方式读取字符串 2083236893 .innerText 或 .innerHTML
在 GM 或 Fiddle 中工作的任何想法或任何工作脚本演示?
您的 XPath 实际上是可以的,如果您直接在浏览器的控制台中输入它应该可以工作。问题在于区块链网页的内容是由脚本动态生成的,因此您要查找的节点在执行用户脚本时不存在。
您需要将代码从 运行ning 延迟到页面加载完成之后。 @run-at 用户脚本元数据指令应该可以实现此目的,但要么它在 GreaseMonkey v4 中被破坏,要么 Blockchain 脚本花费太长时间 运行 并且您需要将您的用户脚本延迟更长时间。无论哪种方式,将您的代码包装在 setTimeout()
中是实现该技巧的简单方法(检测其他脚本何时完成会更优雅):
// ==UserScript==
// @name xpath greasemonkey
// @namespace bbb
// @description f
// @match http://srv1.yogh.io/
// @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.snapshotItem(0).innerHTML);
} else {
console.log("Bother...");
}
}, 5000);
(请注意,我还必须更改您的 @match
指令以使 Greasemonkey 成为右侧页面上的 运行 脚本。