feat: better handling for click next navigation

This commit is contained in:
RohitR311
2025-01-11 17:51:31 +05:30
parent 73bbdeb6bb
commit 7aa7dc27b3

View File

@@ -577,7 +577,7 @@ export default class Interpreter extends EventEmitter {
previousHeight = currentTopHeight; previousHeight = currentTopHeight;
break; break;
case 'clickNext': case 'clickNext':
console.log("PAGE URL:", page.url()); console.log("Page URL:", page.url());
const pageResults = await page.evaluate((cfg) => window.scrapeList(cfg), config); const pageResults = await page.evaluate((cfg) => window.scrapeList(cfg), config);
// console.log("Page results:", pageResults); // console.log("Page results:", pageResults);
@@ -591,6 +591,7 @@ export default class Interpreter extends EventEmitter {
}); });
allResults = allResults.concat(newResults); allResults = allResults.concat(newResults);
console.log("Results so far:", allResults.length);
if (config.limit && allResults.length >= config.limit) { if (config.limit && allResults.length >= config.limit) {
return allResults.slice(0, config.limit); return allResults.slice(0, config.limit);
@@ -623,31 +624,52 @@ export default class Interpreter extends EventEmitter {
const selectorIndex = availableSelectors.indexOf(workingSelector!); const selectorIndex = availableSelectors.indexOf(workingSelector!);
availableSelectors = availableSelectors.slice(selectorIndex); availableSelectors = availableSelectors.slice(selectorIndex);
// await Promise.all([
// nextButton.dispatchEvent('click'),
// page.waitForNavigation({ waitUntil: 'networkidle' })
// ]);
const initialUrl = page.url();
let navigationSuccessful = false;
try { try {
// First try with regular click // Start watching for navigation before clicking
await Promise.all([ const navigationPromise = page.waitForNavigation({
nextButton.click(), waitUntil: 'networkidle',
page.waitForNavigation({ waitUntil: 'networkidle' }) timeout: 30000
]); });
await page.waitForTimeout(1000); // Perform the click
await nextButton.click();
// Wait for navigation to complete
await navigationPromise;
navigationSuccessful = true;
} catch (clickError) { } catch (clickError) {
console.log('Regular click failed, trying dispatchEvent'); console.log('Initial navigation attempt failed:', clickError.message);
}
if (!navigationSuccessful) {
try { try {
// Fallback to dispatchEvent // Start watching for navigation before the event
await Promise.all([ const navigationPromise = page.waitForNavigation({
nextButton.dispatchEvent('click'), waitUntil: 'networkidle',
page.waitForNavigation({ waitUntil: 'networkidle' }) timeout: 30000
]); });
await page.waitForTimeout(1000); await nextButton.dispatchEvent('click');
} catch (navigationError) { await navigationPromise;
console.log(`Navigation failed with selector ${workingSelector}:`); } catch (dispatchError) {
availableSelectors.shift(); console.log(`Navigation failed with selector ${workingSelector}:`, dispatchError.message);
continue; // Check if we actually navigated despite the error
if (page.url() === initialUrl) {
continue; // Only continue if we're still on the same page
}
} }
} }
// Give the page a moment to stabilize after navigation
await page.waitForTimeout(1000);
break; break;
case 'clickLoadMore': case 'clickLoadMore':
while (true) { while (true) {