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;
}
function getSelectorPath(element: HTMLElement | null, stopAtParent: HTMLElement | null): string {
const path: string[] = [];
while (element && element !== stopAtParent && element !== document.body) {
const selector = getNonUniqueSelector(element);
path.unshift(selector);
element = element.parentElement;
}
return path.join(' > ');
function getSelectorPath(element: HTMLElement | null): string {
if (!element || !element.parentElement) return '';
const parentSelector = getNonUniqueSelector(element.parentElement);
const elementSelector = getNonUniqueSelector(element);
return `${parentSelector} > ${elementSelector}`;
}
function getAllDescendantSelectors(element: HTMLElement, stopAtParent: HTMLElement | null): string[] {
let selectors: string[] = [];
const children = Array.from(element.children) as HTMLElement[];
for (const child of children) {
selectors.push(getSelectorPath(child, stopAtParent));
selectors.push(getSelectorPath(child));
selectors = selectors.concat(getAllDescendantSelectors(child, stopAtParent));
}