From c587a3264b1758efee509644bb46203e60275780 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Tue, 3 Sep 2024 09:26:07 +0530 Subject: [PATCH] feat: generate selectors relative to the same parent element as getNonUniqueSelectors --- server/src/workflow-management/selector.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/src/workflow-management/selector.ts b/server/src/workflow-management/selector.ts index 40dd414b..f8ab5482 100644 --- a/server/src/workflow-management/selector.ts +++ b/server/src/workflow-management/selector.ts @@ -806,10 +806,10 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro return selector; } - function getSelectorPath(element: HTMLElement | null): string { + function getSelectorPath(element: HTMLElement | null, stopAtParent: HTMLElement | null): string { const path: string[] = []; - while (element && element !== document.body) { + while (element && element !== stopAtParent && element !== document.body) { const selector = getNonUniqueSelector(element); path.unshift(selector); element = element.parentElement; @@ -818,13 +818,13 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro return path.join(' > '); } - function getAllDescendantSelectors(element: HTMLElement): string[] { + 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)); - selectors = selectors.concat(getAllDescendantSelectors(child)); + selectors.push(getSelectorPath(child, stopAtParent)); + selectors = selectors.concat(getAllDescendantSelectors(child, stopAtParent)); } return selectors; @@ -833,7 +833,7 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro const parentElement = document.querySelector(parentSelector) as HTMLElement; if (!parentElement) return []; - return getAllDescendantSelectors(parentElement); + return getAllDescendantSelectors(parentElement, parentElement); }, parentSelector); return childSelectors || [];