just wondering why Puppeteer wont click an element for me...
the code is as follows:
var clickPhone = '//*[contains(@class, "ep-epage-sidebar__phone-button")]';
var showPhone = '//*[contains(@class, "ep-epage-phone-popup-number__button")]';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(1200000);
await page.goto(url);
const cp = await page.$x(clickPhone);
if (cp) {
page.click(cp)
}
const sp = await page.$x(showPhone);
if (sp) {
page.click(sp);
}
everything is imported correctly above and below is just formatting. But I need it to click through a button for me, which will open a modal, it will then click a button on that modal and scrape the data from there.
However I am met with a few different errors whenever I try and fix the code.
The errors that tend to be met are:
startsWith() is not a function. Not a selector.
I have it to the point now where this code just runs a 'blank run', no output is received as 'cp' and 'sp' are both false due to errors, hence it doesn't even attempt to click anymore.
CodePudding user response:
I think the $x function returns an array.
Try await cp[0].click()
CodePudding user response:
I’m having the same problem, got a slight fix but may not work 100% of the time.
You can try using document.click()