mirror of
https://github.com/NaiboWang/EasySpider.git
synced 2025-04-20 08:04:59 +08:00
Add New XPath
This commit is contained in:
parent
dd8a080598
commit
5f7aee4918
@ -49,10 +49,31 @@ export function getElementXPaths(element, parentElement = document.body) {
|
|||||||
if (element.alt) {
|
if (element.alt) {
|
||||||
paths.push("//" + element.tagName + "[@alt='" + element.alt + "']");
|
paths.push("//" + element.tagName + "[@alt='" + element.alt + "']");
|
||||||
}
|
}
|
||||||
|
paths.push(getAbsoluteXPathWithReverseIndex(element));
|
||||||
console.log("ALL PATHS: " + paths);
|
console.log("ALL PATHS: " + paths);
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getAbsoluteXPathWithReverseIndex(element) {
|
||||||
|
var path = [];
|
||||||
|
while (element && element.nodeType == Node.ELEMENT_NODE) {
|
||||||
|
var index = 0;
|
||||||
|
for (var sibling = element.nextSibling; sibling; sibling = sibling.nextSibling) {
|
||||||
|
// Ignore document type declaration.
|
||||||
|
if (sibling.nodeType == Node.DOCUMENT_TYPE_NODE)
|
||||||
|
continue;
|
||||||
|
if (sibling.nodeName == element.nodeName)
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tagName = element.nodeName.toLowerCase();
|
||||||
|
var pathIndex = (index ? "[last()-" + index + "]" : "");
|
||||||
|
path.unshift(tagName + pathIndex);
|
||||||
|
|
||||||
|
element = element.parentNode;
|
||||||
|
}
|
||||||
|
return "/" + path.join("/");
|
||||||
|
}
|
||||||
|
|
||||||
//返回element相对node节点的xpath,默认的node节点是: /
|
//返回element相对node节点的xpath,默认的node节点是: /
|
||||||
export function readXPath(element, type = 1, node = document.body) {
|
export function readXPath(element, type = 1, node = document.body) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user