# How to Check an Element's Existence in Playwright

Ensuring an element's presence is a fundamental task in Playwright, crucial for
actions like clicking on the element or determining the test outcome based on
its existence.

Below are several strategies for this purpose:

[summary]
## 🔭 Want to automate and scale your Playwright end-to-end tests?
Head over to [Better Stack](https://betterstack.com/website-monitoring) and start monitoring in 5 minutes.
[/summary]

## 1. Assessing element visibility

To ascertain an element's visibility, apply the `isVisible()` method to a
locator:

```javascript
const visible = await page.getByRole('button').isVisible();
if (visible) {
    // do something
}
```

For direct visibility assertions, utilize `toBeVisible()`:

```javascript
await expect(page.getByRole('button')).toBeVisible();
```

To assert the inverse, that an element is hidden, use the `not` property:

```javascript
await expect(page.getByRole('button')).not.toBeVisible();
```

## 2. Checking DOM presence

To check if an element is present in the DOM, regardless of its visibility, use
the `count()` method:

```javascript
const count = await page.getByRole('button').count();
if (count > 1) {
    // do something
}
```

For precise assertions on an element's presence, `toHaveCount()` is recommended:

```javascript
// Exactly one element matching the locator is present
await expect(page.getByRole('button')).toHaveCount(1);
```

To verify the absence of an element, proceed as follows:

```javascript
await expect(page.getByRole('button')).toHaveCount(0);
```

Thanks for reading, and happy coding!

[ad-uptime]