Skip to content

feat: improve selector generation highlighting #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion maxun-core/src/browserSide/scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,30 @@ function scrapableHeuristics(maxCountPerPage = 50, minArea = 20000, scrolls = 3,
});
}

function tryFallbackSelector(rootElement, originalSelector) {
let element = queryElement(rootElement, originalSelector);

if (!element && originalSelector.includes('nth-child')) {
const match = originalSelector.match(/nth-child\((\d+)\)/);
if (match) {
const position = parseInt(match[1], 10);

for (let i = position - 1; i >= 1; i--) {
const fallbackSelector = originalSelector.replace(/nth-child\(\d+\)/, `nth-child(${i})`);
element = queryElement(rootElement, fallbackSelector);
if (element) break;
}

if (!element) {
const baseSelector = originalSelector.replace(/\:nth-child\(\d+\)/, '');
element = queryElement(rootElement, baseSelector);
}
}
}

return element;
}

// Main scraping logic with context support
let containers = queryElementAll(document, listSelector);
containers = Array.from(containers);
Expand Down Expand Up @@ -902,7 +926,7 @@ function scrapableHeuristics(maxCountPerPage = 50, minArea = 20000, scrolls = 3,
for (const [label, { selector, attribute }] of Object.entries(nonTableFields)) {
// Get the last part of the selector after any context delimiter
const relativeSelector = selector.split(/(?:>>|:>>)/).slice(-1)[0];
const element = queryElement(container, relativeSelector);
const element = tryFallbackSelector(container, relativeSelector);

if (element) {
record[label] = extractValue(element, attribute);
Expand Down
2 changes: 2 additions & 0 deletions maxun-core/src/interpret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ export default class Interpreter extends EventEmitter {
button.click()
]);
debugLog("Navigation successful after regular click");
await page.waitForTimeout(2000);
paginationSuccess = true;
} catch (navError) {
debugLog("Regular click with navigation failed, trying dispatch event with navigation");
Expand All @@ -839,6 +840,7 @@ export default class Interpreter extends EventEmitter {
button.dispatchEvent('click')
]);
debugLog("Navigation successful after dispatch event");
await page.waitForTimeout(2000);
paginationSuccess = true;
} catch (dispatchNavError) {
try {
Expand Down
20 changes: 16 additions & 4 deletions server/src/workflow-management/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,12 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
return selectorParts.join(contextPath[0].type === 'shadow' ? ' >> ' : ' :>> ');
}

// Regular DOM path generation
const elementSelector = getNonUniqueSelector(element);

if (elementSelector.includes('.') && elementSelector.split('.').length > 1) {
return elementSelector;
}

const path: string[] = [];
let currentElement = element;
const MAX_DEPTH = 2;
Expand Down Expand Up @@ -2656,7 +2661,12 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
return selectorParts.join(contextPath[0].type === 'shadow' ? ' >> ' : ' :>> ');
}

// Regular DOM path generation
const elementSelector = getNonUniqueSelector(element);

if (elementSelector.includes('.') && elementSelector.split('.').length > 1) {
return elementSelector;
}

const path: string[] = [];
let currentElement = element;
const MAX_DEPTH = 2;
Expand Down Expand Up @@ -2753,12 +2763,14 @@ export const getChildSelectors = async (page: Page, parentSelector: string): Pro
const frameElement = ownerDocument?.defaultView?.frameElement;
if (frameElement) {
const frameSelector = getNonUniqueSelector(frameElement as HTMLElement);
const isFrame = frameElement.tagName === 'FRAME';
// Use the appropriate delimiter based on whether it's a frame or iframe
return `${frameSelector} :>> ${elementSelector}`;
}

// Regular DOM context
if (elementSelector.includes('.') && elementSelector.split('.').length > 1) {
return elementSelector;
}

const parentSelector = getNonUniqueSelector(element.parentElement);
return `${parentSelector} > ${elementSelector}`;
}
Expand Down