feat: add race condition to handle click action

This commit is contained in:
RohitR311
2025-01-11 19:33:28 +05:30
parent ab07399017
commit 3bb36ae89a

View File

@@ -629,43 +629,29 @@ export default class Interpreter extends EventEmitter {
// page.waitForNavigation({ waitUntil: 'networkidle' }) // page.waitForNavigation({ waitUntil: 'networkidle' })
// ]); // ]);
const initialUrl = page.url(); const previousUrl = page.url();
let navigationSuccessful = false;
try { try {
// Start watching for navigation before clicking // Try both click methods simultaneously
const navigationPromise = page.waitForNavigation({ await Promise.race([
waitUntil: 'networkidle', Promise.all([
timeout: 30000 page.waitForNavigation({ waitUntil: 'networkidle', timeout: 30000 }),
}); nextButton.click()
]),
// Perform the click Promise.all([
await nextButton.click(); page.waitForNavigation({ waitUntil: 'networkidle', timeout: 30000 }),
nextButton.dispatchEvent('click')
// Wait for navigation to complete ])
await navigationPromise; ]);
navigationSuccessful = true; } catch (error) {
} catch (clickError) { // Verify if navigation actually succeeded
console.log('Initial navigation attempt failed:', clickError.message); const currentUrl = page.url();
} if (currentUrl === previousUrl) {
console.log("Previous URL same as current URL. Navigation failed.");
if (!navigationSuccessful) { continue;
try {
// Start watching for navigation before the event
const navigationPromise = page.waitForNavigation({
waitUntil: 'networkidle',
timeout: 30000
});
await nextButton.dispatchEvent('click');
await navigationPromise;
} catch (dispatchError) {
console.log(`Navigation failed with selector ${workingSelector}:`, dispatchError.message);
// Check if we actually navigated despite the error
if (page.url() === initialUrl) {
continue; // Only continue if we're still on the same page
}
} }
// Otherwise, log and continue
console.log('Navigation succeeded despite click error');
} }
// Give the page a moment to stabilize after navigation // Give the page a moment to stabilize after navigation
@@ -704,10 +690,13 @@ export default class Interpreter extends EventEmitter {
// Click the 'Load More' button to load additional items // Click the 'Load More' button to load additional items
try { try {
await loadMoreButton.click(); await Promise.race([
} catch { loadMoreButton.click(),
console.log('Regular click failed, trying dispatchEvent'); loadMoreButton.dispatchEvent('click')
await loadMoreButton.dispatchEvent('click') ]);
} catch (error) {
console.log('Both click attempts failed');
continue;
} }
await page.waitForTimeout(2000); // Wait for new items to load await page.waitForTimeout(2000); // Wait for new items to load
@@ -723,7 +712,7 @@ export default class Interpreter extends EventEmitter {
return allResults; return allResults;
} }
previousHeight = currentHeight; previousHeight = currentHeight;
console.log("Results so far:", allResults.length); console.log("Results so far:", allResults.length);
if (config.limit && allResults.length >= config.limit) { if (config.limit && allResults.length >= config.limit) {
// If limit is set and reached, return the limited results // If limit is set and reached, return the limited results