Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

347
Views
cannot read properties of undefined 'textContent'

I have a simple piece of code

describe('My First Puppeeteer Test', () => {
it('Should launch the browser', async function() {
        const browser = await puppeteer.launch({ headless: false})
        const page = await browser.newPage()
        await page.goto('https://github.com/login')
        await page.type('#login_field', testLogin)
        await page.type('#password', testPassword)
        await page.click('[name="commit"]')
        await page.waitForNavigation()
        
        let [element] = await page.$x('//h3[@class="text-normal"]')
        let helloText = await page.evaluate(element => element.textContent, element);
        
        console.log(helloText);
        browser.close();
    })
})

Everything worked before but today I get an error + my stacktrace:

Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'textContent') at puppeteer_evaluation_script:1:21 at ExecutionContext._evaluateInternal (node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async ExecutionContext.evaluate (node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16) at async Context. (tests\example.tests.js:16:22)

How I can resolve this?

Kind regards

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

While I haven't tested the code due to the login and I assume your selectors are correct, the main problem is almost certainly that

await page.click('[name="commit"]')
await page.waitForNavigation()

creates a race condition. The docs clarify:

Bear in mind that if click() triggers a navigation event and there's a separate page.waitForNavigation() promise to be resolved, you may end up with a race condition that yields unexpected results. The correct pattern for click and wait for navigation is the following:

const [response] = await Promise.all([
  page.waitForNavigation(waitOptions),
  page.click(selector, clickOptions),
]);

As a side point, it's probably better to do waitForXPath rather than $x, although this seems less likely the root problem. Don't forget to await all promises such as browser.close().

const puppeteer = require("puppeteer");

let browser;
(async () => {
  browser = await puppeteer.launch({headless: true});
  const [page] = await browser.pages();
  await page.goto('https://github.com/login');
  await page.type('#login_field', testLogin);
  await page.type('#password', testPassword);

  //    vvvvvvvvvvv
  await Promise.all([
    page.click('[name="commit"]'),
    page.waitForNavigation(),
  ]);
  const el = await page.waitForXPath('//h3[@class="text-normal"]');
                 //     ^^^^^^^^^^^^
  //const el = await page.waitForSelector("h3.text-normal"); // ..or

  const text = await el.evaluate(el => el.textContent);
  console.log(text);
  //await browser.close();
  //^^^^^ missing await, or use finally as below
})()
  .catch(err => console.error(err))
  .finally(() => browser?.close())
;

Additionally, if you're using Jest, once you get things working, you might want to move the browser and page management to beforeEach/afterEach or beforeAll/afterAll blocks. It's faster to use the same browser instance for all test cases, and pages can be opened and closed before/after each case.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!