I've written tiny scripts in node using puppeteer to perform clicks cyclically on the link of different posts from it's landing page of a website.
The site link used within my scripts is a placeholder. Moreover, they are not dynamic. So, puppeteer might be overkill. However, My intention is to learn the logic of clicking.
When I execute my first script, It clicks once and throws the following error as it goes out of the source.
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch({headless:false});
const [page] = await browser.pages();
await page.goto("https://stackoverflow.com/questions/tagged/web-scraping",{waitUntil:'networkidle2'});
await page.waitFor(".summary");
const sections = await page.$$(".summary");
for (const section of sections) {
await section.$eval(".question-hyperlink", el => el.click())
}
await browser.close();
})();
The error the above script encounters:
(node:9944) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
When I execute the following, the script pretends to click once (in reality it is not) and encounters the same error as earlier.
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch({headless:false});
const [page] = await browser.pages();
await page.goto("https://stackoverflow.com/questions/tagged/web-scraping");
await page.waitFor(".summary .question-hyperlink");
const sections = await page.$$(".summary .question-hyperlink");
for (let i=0, lngth = sections.length; i < lngth; i++) {
await sections[i].click();
}
await browser.close();
})();
The error the above one throws:
(node:10128) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
How can I let my script perform clicks cyclically?