---
id: selectors
title: "Element selectors"
---
Selectors are strings that point to the elements in the page. They are used to perform actions on those
elements by means of methods such as [`method: Page.click`], [`method: Page.fill`] and alike. All those
methods accept [`param: selector`] as their first argument.
## Quick guide
- Text selector
```js
await page.click('text=Log in');
```
```python async
await page.click("text=Log in")
```
```python sync
page.click("text=Log in")
```
Learn more about [text selector][text].
- CSS selector
```js
await page.click('button');
await page.click('#nav-bar .contact-us-item');
```
```python async
await page.click("button")
await page.click("#nav-bar .contact-us-item")
```
```python sync
page.click("button")
page.click("#nav-bar .contact-us-item")
```
Learn more about [css selector][css].
- Select by attribute, with css selector
```js
await page.click('[data-test=login-button]');
await page.click('[aria-label="Sign in"]');
```
```python async
await page.click("[data-test=login-button]")
await page.click("[aria-label='Sign in']")
```
```python sync
page.click("[data-test=login-button]")
page.click("[aria-label='Sign in']")
```
Learn more about [css selector][css].
- Combine css and text selectors
```js
await page.click('article:has-text("Playwright")');
await page.click('#nav-bar :text("Contact us")');
```
```python async
await page.click("article:has-text('Playwright')")
await page.click("#nav-bar :text('Contact us')")
```
```python sync
page.click("article:has-text('Playwright')")
page.click("#nav-bar :text('Contact us')")
```
Learn more about [`:has-text()` and `:text()` pseudo classes][text].
- Element that contains another, with css selector
```js
await page.click('.item-description:has(.item-promo-banner)');
```
```python async
await page.click(".item-description:has(.item-promo-banner)")
```
```python sync
page.click(".item-description:has(.item-promo-banner)")
```
Learn more about [`:has()` pseudo class](#selecting-elements-that-contain-other-elements).
- Selecting based on layout, with css selector
```js
await page.click('input:right-of(:text("Username"))');
```
```python async
await page.click("input:right-of(:text('Username'))")
```
```python sync
page.click("input:right-of(:text('Username'))")
```
Learn more about [layout selectors](#selecting-elements-based-on-layout).
- Only visible elements, with css selector
```js
await page.click('.login-button:visible');
```
```python async
await page.click(".login-button:visible")
```
```python sync
page.click(".login-button:visible")
```
Learn more about [`:visible` pseudo-class](#selecting-visible-elements).
- Pick n-th match
```js
await page.click(':nth-match(:text("Buy"), 3)');
```
```python async
await page.click(":nth-match(:text('Buy'), 3)"
```
```python sync
page.click(":nth-match(:text('Buy'), 3)"
```
Learn more about [`:nth-match()` pseudo-class](#pick-n-th-match-from-the-query-result).
- XPath selector
```js
await page.click('xpath=//button');
```
```python async
await page.click("xpath=//button")
```
```python sync
page.click("xpath=//button")
```
Learn more about [XPath selector][xpath].
## Text selector
Text selector locates elements that contain passed text.
```js
await page.click('text=Log in');
```
```python async
await page.click("text=Log in")
```
```python sync
page.click("text=Log in")
```
Text selector has a few variations:
- `text=Log in` - default matching is case-insensitive and searches for a substring. For example `text=Log` matches ``.
```js
await page.click('text=Log in');
```
```python async
await page.click("text=Log in")
```
```python sync
page.click("text=Log in")
```
- `text="Log in"` - text body can be escaped with single or double quotes for case-sensitive match. For example `text="Log"` does not match `` but instead matches `Log in`.
Quoted body follows the usual escaping rules, e.g. use `\"` to escape double quote in a double-quoted string: `text="foo\"bar"`.
```js
await page.click('text="Log in"');
```
```python async
await page.click("text='Log in'")
```
```python sync
page.click("text='Log in'")
```
- `"Log in"` - selector starting and ending with a quote (either `"` or `'`) is assumed to be a text selector. For example, `"Log in"` is converted to `text="Log in"` internally.
```js
await page.click('"Log in"');
```
```python async
await page.click("'Log in'")
```
```python sync
page.click("'Log in'")
```
- `/Log\s*in/i` - body can be a [JavaScript-like regex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) wrapped in `/` symbols. For example, `text=/Log\s*in/i` matches `` and ``.
```js
await page.click('text=/Log\\s*in/i');
```
```python async
await page.click("text=/Log\s*in/i")
```
```python sync
page.click("text=/Log\s*in/i")
```
- `article:has-text("Playwright")` - the `:has-text()` pseudo-class can be used inside a [css] selector. It matches any element containing specified text somewhere inside, possibly in a child or a descendant element. For example, `article:has-text("Playwright")` matches `