feat: include immediate parent of each child element in the selector path

This commit is contained in:
karishmas6
2024-09-03 09:44:38 +05:30
parent c587a3264b
commit 01e57ee5aa

View File

@@ -806,24 +806,22 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
return selector; return selector;
} }
function getSelectorPath(element: HTMLElement | null, stopAtParent: HTMLElement | null): string { function getSelectorPath(element: HTMLElement | null): string {
const path: string[] = []; if (!element || !element.parentElement) return '';
while (element && element !== stopAtParent && element !== document.body) { const parentSelector = getNonUniqueSelector(element.parentElement);
const selector = getNonUniqueSelector(element); const elementSelector = getNonUniqueSelector(element);
path.unshift(selector);
element = element.parentElement; return `${parentSelector} > ${elementSelector}`;
}
return path.join(' > ');
} }
function getAllDescendantSelectors(element: HTMLElement, stopAtParent: HTMLElement | null): string[] { function getAllDescendantSelectors(element: HTMLElement, stopAtParent: HTMLElement | null): string[] {
let selectors: string[] = []; let selectors: string[] = [];
const children = Array.from(element.children) as HTMLElement[]; const children = Array.from(element.children) as HTMLElement[];
for (const child of children) { for (const child of children) {
selectors.push(getSelectorPath(child, stopAtParent)); selectors.push(getSelectorPath(child));
selectors = selectors.concat(getAllDescendantSelectors(child, stopAtParent)); selectors = selectors.concat(getAllDescendantSelectors(child, stopAtParent));
} }