feat: less brittle non unique selectors
This commit is contained in:
@@ -730,37 +730,41 @@ export const getSelectors = async (page: Page, coordinates: Coordinates) => {
|
|||||||
* @returns {Promise<Selectors|null|undefined>}
|
* @returns {Promise<Selectors|null|undefined>}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates) => {
|
export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates) => {
|
||||||
try {
|
try {
|
||||||
const selectors = await page.evaluate(({ x, y }) => {
|
const selectors = await page.evaluate(({ x, y }) => {
|
||||||
function getSelector(element: Element): string {
|
function getSelector(element: any) {
|
||||||
let selector = element.tagName.toLowerCase();
|
let selector = element.tagName.toLowerCase();
|
||||||
|
|
||||||
|
// Capture a single, relevant class if present
|
||||||
if (element.className) {
|
if (element.className) {
|
||||||
const classes = element.className.split(/\s+/).filter(Boolean);
|
const classes = element.className.split(/\s+/).filter(Boolean);
|
||||||
if (classes.length > 0) {
|
if (classes.length > 0) {
|
||||||
selector += '.' + classes.join('.');
|
// Use only the first class to avoid over-specificity
|
||||||
|
selector += '.' + classes[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return selector;
|
return selector;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSelectorPath(element: Element): string {
|
function getSelectorPath(element: any) {
|
||||||
const path = [];
|
const path = [];
|
||||||
while (element && element !== document.body) {
|
while (element && element !== document.body) {
|
||||||
path.unshift(getSelector(element));
|
const selector = getSelector(element);
|
||||||
element = element.parentElement!;
|
path.unshift(selector);
|
||||||
|
element = element.parentElement;
|
||||||
}
|
}
|
||||||
return path.join(' > ');
|
return path.join(' > ');
|
||||||
}
|
}
|
||||||
|
|
||||||
const element = document.elementFromPoint(x, y) as Element;
|
const element = document.elementFromPoint(x, y);
|
||||||
if (!element) return null;
|
if (!element) return null;
|
||||||
|
|
||||||
const generalSelector = getSelectorPath(element);
|
const generalSelector = getSelectorPath(element);
|
||||||
return {
|
return {
|
||||||
generalSelector,
|
generalSelector,
|
||||||
};
|
};
|
||||||
|
|
||||||
}, coordinates);
|
}, coordinates);
|
||||||
|
|
||||||
return selectors || {};
|
return selectors || {};
|
||||||
@@ -770,6 +774,7 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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