7 solutions for page.click() is not working in puppeteer
Sometimes while running puppeteer script automation engineers are facing issues on that page.click() / element.click() is not working correctly. Here two types of issues coming
- Page.click() fails with exception
- page. click() fails to click the element and also not throwing any error.
To understand proper deep use cases and how to resolve these errors you can go throw the below youtube video and subscribe to the channel
1. Page.click() fails with exception
Sometimes, page.click() fails and gives some exceptions like elementNotFound or similar. The main reasons could be
- Missed Await - make sure you have used await() to make sure the script runs in the correct order.
- Not used page.waitforElement() - To make sure element puppeteer waits for an element to show up on page before it clicks. Sometimes we miss to waitForElement while clicking in the dropdown or elements that appear after a click.
2. Page.click fails and not throwing any exception in puppeteer
Sometimes, Script went through the page. click and you can see it's not clicking on the element and not throwing any error in the console either. Also, it happens, when you are debugging it works but while running it keeps on failing. Sometimes it is not failing every time. Anyway, you can check the above two solutions. further, you can try below -
1. Focus and Enter - First Focus on the element and then press enter.
await page.focus('Element path' )
await page.keyboard.type('\n');
2. using $eval - you can try as below, it will work in most of the cases.
await page.$eval('#link', elem => elem.click());
3. Using mouse click -
const box = element.boundingBox();
const x = box.x + (box.width/2);
const y = box.y + (box.height/2);
page.mouse.move(x,y);
page.mouse.down();
sleep(delay);
page.mouse.up();
4. Using javascript using document class - Most probably this will work everytime. But one should not use this open. (Have mentioned reasons in the above video in detail).
document.getElementById("myCheck").click();
5. Using Static wait
At last, you can use static wait to make sure element gets visible and clickable, and then puppeteer clicks on it. You can watch the above video for how you should use static waits.
To understand each and every point why and how, please watch the video from my youtube channel and subscribe to the channel -> Youtube channel. Thank You.
No comments