slow click (and events) performance on puppteer

Viewed 31

I have a small automation code that works on a website, using puppeteer, chrome headless. The code is very simple.

locate an element.
click on it.

the thing is that the click events are SUPER slow, I assume it should take less than a few ms.

but it takes 90-400ms to perform a single click event. there is no need to scroll because all the elements appear on the screen. here is a piece of code:

logger.verbose(`before clicking on row`);
// get by link text
// search for any element with this class and text
let workElement = await page.$x(`//span[contains(@class,"tr-class") and contains(text(),"${jobSearchId}")]`);
//we don't want to click on the element itself because it makes things more difficult
let row = await (await workElement.getProperty("parentNode")).getProperty("parentNode");
await row.click();
logger.verbose(`row clicked`);
//click on the button
logger.verbose(`button click before`);
await page.click(".searchJobDescription");
logger.verbose(`button clicked`);
0|main  | 2022-08-17 13:09:05.283 - verbose: before clicking on row
0|main  | 2022-08-17 13:09:05.685 - verbose: row clicked
0|main  | 2022-08-17 13:09:05.685 - verbose: button click before
0|main  | 2022-08-17 13:09:05.776 - verbose: button clicked

clicking on the row takes 402ms
and clicking the button takes 91ms

even a human can perform these actions much faster.

can anyone help me understand how to speed up these actions? puppeteer is very slow and I don't know why.

1 Answers

So...
The solution is super weird and seems not related to the problem, but I have tested it again and again, and I'm 100% sure that this is the cause (or part of a bigger bug I can't see).

I have built a cookies file that stores the page cookies, when I am injecting the cookies back to the page, everything slows down, I am using the next method for it.

async function injectCookies() {
  const cookiesList = getCookiesFromJarAsList();
  await page.setCookie(...cookiesList);
}

when I am removing the call for injecting the cookies, puppeteer runs fast again. (~30ms per click)

async function injectCookies() {
  const cookiesList = getCookiesFromJarAsList();
  // removing this line makes Puppeteer great again
  // await page.setCookie(...cookiesList);
}

I am not even sure that this is a puppeteer issue. it seems like Chrome/Chromium problem because when I debugged the CDP, chrome itself returned very slow answers for the command "mouseMove" (which is part of Puppeteer click method).

not sure what's going on, so if someone smart along the way will be able to solve it, it'll be great.

For me the problem been solved and I wasted more then a week in debugging it, so I'm leaving it this way.

if someone can reproduce it, and open a bug for puppeteer/chromium guys, I am sending him very good Karma.

Related