Skip to content

Proposal to make script more stable #14

@usaerc

Description

@usaerc

I found delay a bit short:
await window.delay(1000);
//await window.delay(150);

Other fix for pages sometimes not firing load event
// Added to prevent stopping
async function gotoWithRetry(page, url, options, retries = 5, timeout = 10000) {
let attempts = 0;
while (attempts < retries) {
try {
// Start navigation
const response = await Promise.race([
page.goto(url, options), // Attempt to go to the URL
new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), timeout)) // Timeout promise
]);

        // Check if the response is valid
        if (response && response.ok()) {
            //console.log('Page loaded successfully!');
            return; // Successfully loaded the page
        } else {
            throw new Error('Failed to load the page.');
        }
    } catch (error) {
        attempts++;
        console.error(`Attempt ${attempts} failed: ${error.message}`);
        if (attempts === retries) {
            throw new Error(`Failed to load the page after ${retries} attempts.`);
        }
        console.log('Retrying...');
    }
}

}

replace

await gotoWithRetry(page, url, { waitUntil: 'load' });
//await page.goto(url, { waitUntil: "load" });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions