I am trying to scrape a real estate website. This is my first time using Puppeteer. Up to now I have managed to open the link and click on the "accept all" button for cookies. Now I am trying to click on the "sign in" button. This is my code:
const puppeteer = require("puppeteer");
const link1 =
"https://www.daft.ie/for-rent/walled-garden-wyckham-way-dundrum-dublin-14/3988400";
(async () => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
});
const page = await browser.newPage();
await page.goto(link1);
const elements = await page.$x("//button[normalize-space()='Accept All']");
await elements[0].click();
console.log("cookies accepted");
const signin = await page.$x("//a[contains(., 'Sign in')]");
await signin[0].click();
console.log("Sign in clicked");
await browser.close();
})();
This is the error I get after I run the script:
throw new Error('Execution context was destroyed, most likely because of a navigation.');
^
Error: Execution context was destroyed, most likely because of a navigation.
at rewriteError (/Users/devang/Desktop/coding/daft/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:280:15)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ExecutionContext._ExecutionContext_evaluate (/Users/devang/Desktop/coding/daft/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:226:56)
at async ElementHandle.evaluateHandle (/Users/devang/Desktop/coding/daft/node_modules/puppeteer/lib/cjs/puppeteer/common/JSHandle.js:97:16)
at async Object.internalHandler.queryAll (/Users/devang/Desktop/coding/daft/node_modules/puppeteer/lib/cjs/puppeteer/common/QueryHandler.js:63:30)
at async ElementHandle.$$ (/Users/devang/Desktop/coding/daft/node_modules/puppeteer/lib/cjs/puppeteer/common/ElementHandle.js:116:17)
at async /Users/alex/Desktop/coding/daft/index.js:18:1
I verified the xPath I am using for the a tag sign in button is correct.
Here's the HTML of the sign in link:
<a data-testid="top-level-active-nav-link" href="/auth/authenticate" class="Navigationstyled__TopLevelItemActiveLink-sc-1tagpb7-6 bsFoce">Sign in</a>
What am I doing wrong here?
Link of the target page - list