feat: traverse up the parent selector
This commit is contained in:
@@ -559,6 +559,8 @@ export class WorkflowGenerator {
|
|||||||
if (this.listSelector !== '') {
|
if (this.listSelector !== '') {
|
||||||
const childSelectors = await getChildSelectors(page, this.listSelector || '');
|
const childSelectors = await getChildSelectors(page, this.listSelector || '');
|
||||||
this.socket.emit('highlighter', { rect, selector: displaySelector, elementInfo, childSelectors })
|
this.socket.emit('highlighter', { rect, selector: displaySelector, elementInfo, childSelectors })
|
||||||
|
console.log(`Child Selectors: ${childSelectors}`)
|
||||||
|
console.log(`List sekector ${this.listSelector}`)
|
||||||
} else {
|
} else {
|
||||||
this.socket.emit('highlighter', { rect, selector: displaySelector, elementInfo });
|
this.socket.emit('highlighter', { rect, selector: displaySelector, elementInfo });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -790,20 +790,21 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export const getChildSelectors = async (page: Page, parentSelector: string): Promise<string[]> => {
|
export const getChildSelectors = async (page: Page, parentSelector: string): Promise<string[]> => {
|
||||||
try {
|
try {
|
||||||
const childSelectors = await page.evaluate((parentSelector: string) => {
|
const childSelectors = await page.evaluate((parentSelector: string) => {
|
||||||
|
// Function to generate non-unique selector for an element
|
||||||
function getNonUniqueSelector(element: HTMLElement): string {
|
function getNonUniqueSelector(element: HTMLElement): string {
|
||||||
let selector = element.tagName.toLowerCase();
|
let selector = element.tagName.toLowerCase();
|
||||||
|
|
||||||
|
// Add class names if available
|
||||||
const className = typeof element.className === 'string' ? element.className : '';
|
const className = typeof element.className === 'string' ? element.className : '';
|
||||||
if (className) {
|
if (className) {
|
||||||
const classes = className.split(/\s+/).filter((cls: string) => Boolean(cls));
|
const classes = className.split(/\s+/).filter((cls: string) => Boolean(cls));
|
||||||
if (classes.length > 0) {
|
if (classes.length > 0) {
|
||||||
const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':'));
|
const validClasses = classes.filter((cls: string) => !cls.startsWith('!') && !cls.includes(':'));
|
||||||
if (validClasses.length > 0) {
|
if (validClasses.length > 0) {
|
||||||
selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.');
|
selector += '.' + validClasses.map(cls => CSS.escape(cls)).join('.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -811,30 +812,34 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
|
|||||||
return selector;
|
return selector;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSelectorPath(element: HTMLElement | null): string {
|
// Function to get the full CSS selector path from the current element to the parentSelector
|
||||||
|
function getSelectorPath(element: HTMLElement | null, parentElement: HTMLElement): string {
|
||||||
if (!element || !element.parentElement) return '';
|
if (!element || !element.parentElement) return '';
|
||||||
|
|
||||||
const parentSelector = getNonUniqueSelector(element.parentElement);
|
const parentSelector = getNonUniqueSelector(parentElement);
|
||||||
const elementSelector = getNonUniqueSelector(element);
|
const elementSelector = getNonUniqueSelector(element);
|
||||||
|
|
||||||
return `${parentSelector} > ${elementSelector}`;
|
return `${parentSelector} > ${elementSelector}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAllDescendantSelectors(element: HTMLElement, stopAtParent: HTMLElement | null): string[] {
|
// Function to recursively gather all descendant selectors
|
||||||
|
function getAllDescendantSelectors(element: HTMLElement, parentElement: HTMLElement): 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));
|
selectors.push(getSelectorPath(child, parentElement));
|
||||||
selectors = selectors.concat(getAllDescendantSelectors(child, stopAtParent));
|
selectors = selectors.concat(getAllDescendantSelectors(child, parentElement));
|
||||||
}
|
}
|
||||||
|
|
||||||
return selectors;
|
return selectors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the parent element based on the selector
|
||||||
const parentElement = document.querySelector(parentSelector) as HTMLElement;
|
const parentElement = document.querySelector(parentSelector) as HTMLElement;
|
||||||
if (!parentElement) return [];
|
if (!parentElement) return [];
|
||||||
|
|
||||||
|
// Gather all descendant selectors starting from the parent element
|
||||||
return getAllDescendantSelectors(parentElement, parentElement);
|
return getAllDescendantSelectors(parentElement, parentElement);
|
||||||
}, parentSelector);
|
}, parentSelector);
|
||||||
|
|
||||||
@@ -845,6 +850,7 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the first pair from the given workflow that contains the given selector
|
* Returns the first pair from the given workflow that contains the given selector
|
||||||
* inside the where condition, and it is the only selector there.
|
* inside the where condition, and it is the only selector there.
|
||||||
|
|||||||
Reference in New Issue
Block a user