get element text puppeteer 1

get element text puppeteer

 const puppeteer = require('puppeteer');

describe('My First Puppeteer Test', () => {
    it('should launch the browser', async function() {
        const browser = await puppeteer.launch({
             headless: false,
             slowMo: 50, 
             devtools: false,
        });
        const page = await browser.newPage();
        await page.goto('http://example.com/');
        const title = await page.title();
        const url = await page.url();
        const text = await page.$eval('h1', element => element.textContent)
        console.log('Text in the H1: ' + text);
        await browser.close();
    })
})

Here is what the above code is Doing:
1. We are launching the browser with the headless option set to false. This will open a new browser window.
2. We are opening a new page in the browser.
3. We are navigating to the example.com website.
4. We are getting the title of the page.
5. We are getting the URL of the page.
6. We are getting the text of the h1 element on the page.
7. We are closing the browser.

Similar Posts