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

775
Views
How to check if an element is in the document with playwright?

I want to test if an element had been rendered. So I want expect that if is present. Is there a command for this?

await page.goto(‘<http://localhost:3000/>');
const logo = await page.$(‘.logo’)

// expect(logo.toBeInDocument())
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Since the use of ElementHandle (page.$(), page.$$()) is discouraged by the Playwright Team, you could use the Locator object and the count() method:

expect(await page.locator('data-testid=exists').count()).toBeTruthy();
expect(await page.locator('data-testid=doesnt-exist').count()).toBeFalsy();

If you want to check if the element is rendered (I assume you mean visible) you could use the toBeVisible assertion:

await expect(page.locator('data-testid=is-visible')).toBeVisible();
over 4 years ago · Santiago Trujillo Report

0

If you query one element with page.$(), you can simply use:

const logo = await page.$('.logo');
if (logo) {

}

Similarly if you query multiple elements with page.$$():

const logo = await page.$$('.logo');
if (logo) {

}

Since this example returns (after awaiting) an array of element handles, you can also use property length in the condition:

const logo = await page.$$('.logo');
if (logo.length) {

}

The key in all these examples is to await the promise that page.$() and page.$$() return.

over 4 years ago · Santiago Trujillo Report

0

await expect(page.locator('.logo')).toHaveCount(1)
over 4 years ago · Santiago Trujillo 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!