feat: get all descendant selectors
This commit is contained in:
@@ -784,12 +784,9 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
|
|||||||
return { generalSelector: '' };
|
return { generalSelector: '' };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
interface SelectorNode {
|
|
||||||
selector: string;
|
|
||||||
children: SelectorNode[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getChildSelectors = async (page: Page, parentSelector: string, maxDepth: number = 2): Promise<SelectorNode[]> => {
|
|
||||||
|
export const getChildSelectors = async (page: Page, parentSelector: string, maxDepth: number = 2): Promise<string[]> => {
|
||||||
try {
|
try {
|
||||||
const childSelectors = await page.evaluate(({ parentSelector, maxDepth }: { parentSelector: string, maxDepth: number }) => {
|
const childSelectors = await page.evaluate(({ parentSelector, maxDepth }: { parentSelector: string, maxDepth: number }) => {
|
||||||
function getNonUniqueSelector(element: HTMLElement): string {
|
function getNonUniqueSelector(element: HTMLElement): string {
|
||||||
@@ -822,22 +819,24 @@ export const getChildSelectors = async (page: Page, parentSelector: string, maxD
|
|||||||
return path.join(' > ');
|
return path.join(' > ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDescendantSelectors(element: HTMLElement, currentDepth: number = 0): SelectorNode[] {
|
function getAllDescendantSelectors(element: HTMLElement, currentDepth: number = 0): string[] {
|
||||||
if (currentDepth >= maxDepth) return [];
|
if (currentDepth >= maxDepth) return [];
|
||||||
|
|
||||||
|
let selectors: string[] = [];
|
||||||
const children = Array.from(element.children) as HTMLElement[];
|
const children = Array.from(element.children) as HTMLElement[];
|
||||||
return children.map(child => {
|
|
||||||
return {
|
for (const child of children) {
|
||||||
selector: getSelectorPath(child),
|
selectors.push(getSelectorPath(child));
|
||||||
children: getDescendantSelectors(child, currentDepth + 1)
|
selectors = selectors.concat(getAllDescendantSelectors(child, currentDepth + 1));
|
||||||
};
|
}
|
||||||
});
|
|
||||||
|
return selectors;
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentElement = document.querySelector(parentSelector) as HTMLElement;
|
const parentElement = document.querySelector(parentSelector) as HTMLElement;
|
||||||
if (!parentElement) return [];
|
if (!parentElement) return [];
|
||||||
|
|
||||||
return getDescendantSelectors(parentElement);
|
return getAllDescendantSelectors(parentElement);
|
||||||
}, { parentSelector, maxDepth });
|
}, { parentSelector, maxDepth });
|
||||||
|
|
||||||
return childSelectors || [];
|
return childSelectors || [];
|
||||||
|
|||||||
Reference in New Issue
Block a user