Comparing Puppeteer's page.evaluate() and $ methods: Which is better?

Hey everyone! I’ve been playing around with Puppeteer and I’m trying to figure out the best way to interact with web elements. I’ve got two different approaches, and I’m not sure which one to use.

Here’s the first way:

let element = await page.$('button.submit');
let value = await element.getProperty('value');
await element.click();

And here’s the second way:

await page.evaluate(() => {
  let element = document.querySelector('button.submit');
  let value = element.value;
  element.click();
});

I find the second method easier to understand, but I’m wondering if there are any benefits to using Puppeteer’s $ methods. Are they faster? More reliable? I couldn’t find much info in the docs about why we should use one over the other.

Has anyone here used both approaches? What are your thoughts on the pros and cons of each? I’d love to hear about your experiences and any tips you might have. Thanks!

I’ve worked extensively with both approaches in Puppeteer, and each has its strengths. The $ method is generally more efficient for simple operations, as it operates directly within Node.js. This can lead to faster execution times, especially for basic element selection and manipulation.

However, page.evaluate() shines when dealing with complex DOM interactions or when you need to leverage browser-specific APIs. It allows you to execute JavaScript in the context of the page, which can be crucial for certain scenarios.

In terms of reliability, both methods are generally stable, but $ can sometimes be more predictable as it’s less affected by page-specific JavaScript environments. Ultimately, the choice depends on your specific use case and performance requirements. I’d recommend benchmarking both approaches in your particular application to determine which works best for you.

hey flyingleaf, i used both before. in my view, page.evaluate works best for complex stuff since it runs in the browser, but $ is faster for simple tasks and makes things easier on the node side. try em and see what works best for ur comp!

I’ve been using Puppeteer for a while now, and I’ve found that both approaches have their place. The $ method is great for quick, straightforward tasks. It’s more intuitive if you’re coming from a jQuery background and it’s generally faster for simple operations.

On the flip side, page.evaluate() is my go-to for more complex scenarios. It’s particularly useful when you need to interact with the page’s JavaScript context or perform multiple operations in sequence without the overhead of multiple round-trips between Node and the browser.

One thing to keep in mind is that page.evaluate() runs in the browser context, so you can’t directly access variables from your Node environment inside it. This can make debugging a bit trickier sometimes.

In practice, I often end up using a mix of both methods in my projects, depending on what I’m trying to achieve. My advice would be to start with $ for simple tasks, and switch to page.evaluate() when you need more flexibility or are dealing with more complex page interactions.