-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxpathFinder.js
55 lines (44 loc) · 1.43 KB
/
xpathFinder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
document.addEventListener('click',(e)=>generationElement(e.target));
function getElementByXpath(path) {
return document.evaluate(path, document.body, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
function generationElement(element) {
console.log(element);
if(element.textContent.trim() != "" && element.tagName)
{
var elementType = element.tagName.toLowerCase();
var textContent = element.textContent.trim();
var path = "//" + elementType + "[contains(text(),normalize-space('"+textContent+"'))]";
if(getElementByXpath(path)!= null)
{console.log(path);}
else
{
console.log("Finding child element");
childContextElement(element)
}
}
else{
console.log(" tagname ve content yok");
contextElementFind(element);
}
}
function contextElementFind(element){
element = getPreviousSibling(element);
generationElement(element);
}
function getPreviousSibling(element){
var sibling = element.parentNode;
while (sibling.textContent.trim() === "") {
sibling = sibling.parentNode;
}
return sibling;
}
function childContextElement(element){
var childElement = element.children;
if(childElement.length>0){
for (let i = 0; i < childElement.length; i++) {
if(childElement[i].textContent.trim()!="")
generationElement(childElement[i]);
}
}
}