# How to Get an Element's Attribute in Playwright

Playwright provides a native `getAttribute()` method on a locator for retrieving
the attribute of any element:

```javascript
const dataActionValue = await page.getByRole('button').getAttribute('data-action');
```

To make assertions on the value of an attribute, use `toHaveAttribute()` as
follows:

```javascript
await expect(page.getByRole('button')).toHaveAttribute('data-action', 'cancel');
```

Thanks for reading, and happy coding!

[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]

[ad-uptime]