(element)
| 5 | * Returns array of unique, validated XPaths |
| 6 | */ |
| 7 | export function getXpaths(element) { |
| 8 | if (!element || !element.tagName) return []; |
| 9 | |
| 10 | var paths = []; |
| 11 | paths.push(getElementInfo_Custom(element, false)); |
| 12 | paths.push(getElementInfo_Custom(element, true)); |
| 13 | paths.push(getElementInfo_Moz(element)); |
| 14 | |
| 15 | // Deduplicate paths |
| 16 | var unique = paths.filter(function(path, index, self) { |
| 17 | return path && self.indexOf(path) === index; |
| 18 | }); |
| 19 | |
| 20 | // Validate each XPath |
| 21 | unique = unique.filter(function(xpath) { |
| 22 | try { |
| 23 | var result = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null); |
| 24 | var count = 0; |
| 25 | var node = result.iterateNext(); |
| 26 | while (node) { |
| 27 | count++; |
| 28 | node = result.iterateNext(); |
| 29 | } |
| 30 | return count === 1; // Only return unique XPaths |
| 31 | } catch (e) { |
| 32 | return false; |
| 33 | } |
| 34 | }); |
| 35 | |
| 36 | return unique; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Custom XPath generation with optional attribute-based targeting |
no test coverage detected