How to interact with DOM elements in Puppeteer?

Puppeteer is a Node library that provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It's a powerful tool for web scraping and automated testing of web pages. Here's how to interact with DOM elements in Puppeteer.

1. Getting DOM Elements:

To get a DOM element, you can use the page.$() function, which is equivalent to document.querySelector(). Here's an example:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  const titleElement = await page.$('h1');
  await browser.close();
})();

2. Getting Content of DOM Elements:

You can retrieve the content of a DOM element using the page.evaluate() function. The first argument to this function is a JavaScript function that will be executed in the browser context. This function can take a DOM element as an argument, and should return the content you want to extract. Here's an example:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  const titleElement = await page.$('h1');
  const title = await page.evaluate(el => el.textContent, titleElement);
  console.log(title);  // Prints the text content of the <h1> element
  await browser.close();
})();

3. Clicking on DOM Elements:

You can click on a DOM element using the elementHandle.click() function. Here's an example:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  const button = await page.$('button');
  await button.click();
  await browser.close();
})();

4. Filling Input Fields:

You can fill an input field using the elementHandle.type() function. Here's an example:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  const input = await page.$('input');
  await input.type('Hello, World!');
  await browser.close();
})();

Remember that all these operations are asynchronous and return Promises, so you should use the await keyword to wait for them to complete. Also, don't forget to close the browser when you're done to free up resources.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon