From 01e57ee5aaaebb0e544f822914ebab135725d7ba Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Tue, 3 Sep 2024 09:44:38 +0530 Subject: [PATCH] feat: include immediate parent of each child element in the selector path --- server/src/workflow-management/selector.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/server/src/workflow-management/selector.ts b/server/src/workflow-management/selector.ts index f8ab5482..982bd664 100644 --- a/server/src/workflow-management/selector.ts +++ b/server/src/workflow-management/selector.ts @@ -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)); }