I have this little puppeatear script, but for some reason, it keeps crashing at a random point each time with this error:
TargetCloseError: Protocol error (DOM.describeNode): Target closed
const extractDataFromPage = async () => {
// Select all question containers
const containers = await page.$$("#examQuestions > .container");
for (const container of containers) {
try {
const question = await container.$eval("#questionText", (el) =>
el.textContent.trim()
);
const answers = await container.$$eval(
".answers-container .answer-box",
(answerBoxes) => answerBoxes.map((box) => box.textContent.trim())
);
const correctAnswerIndex = await container.$eval(
"#answersbutton button[data-correct-answer]",
(button) => parseInt(button.getAttribute("data-correct-answer"), 10)
);
const correctAnswer = answers[correctAnswerIndex - 1];
const imageSrc = await container
.$eval(".image-container img", (img) => img.getAttribute("src"))
.catch(() => null);
const imageURL = imageSrc ? `https:/starti.ge/${imageSrc}` : null;
console.log({
question,
answers,
correctAnswer,
imageURL,
});
} catch (error) {
console.error("Error processing container:", error);
}
}
};
// Process each page
const totalPages = 55;
for (let currentPage = 1; currentPage <= totalPages; currentPage++) {
console.log(`Processing page ${currentPage}...`);
await extractDataFromPage();
await delay(5000)
// Go to the next page if not the last page
if (currentPage < totalPages) {
await page.click("#next");
await page.waitForSelector("#examQuestions > .container", {
timeout: 5000,
});
}
}
await browser.close();