Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use the evaluate method on the page object to execute JavaScript code within the context of the page and retrieve the value of a DOM element. Here is an example code snippet to fetch the value within a span tag using Puppeteer:

const puppeteer = require('puppeteer');

async function getValue() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto('https://example.com');

  const elementValue = await page.evaluate(() => {
    // Replace "span-selector" with your selector
    const span = document.querySelector('span-selector');
    return span.textContent;
  });

  console.log(elementValue);

  await browser.close();
}

getValue();

In the above example, querySelector is used to select the element you want to fetch the value of, and textContent is used to retrieve the text content of the selected element. Replace "span-selector" with the appropriate selector for the span tag you want to fetch the value of.