Skip to content

Commit

Permalink
docs: more clarity in the attribute selectors (#5621)
Browse files Browse the repository at this point in the history
Fixes #5615
  • Loading branch information
aslushnikov authored Feb 25, 2021
1 parent f7e5db4 commit b285936
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion docs/src/selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,42 @@ converts `'//html/body'` to `'xpath=//html/body'`.

## id, data-testid, data-test-id, data-test selectors

Attribute engines are selecting based on the corresponding attribute value. For example: `data-test-id=foo` is equivalent to `css=[data-test-id="foo"]`, and `id:light=foo` is equivalent to `css:light=[id="foo"]`.
Playwright supports a shorthand for selecting elements using certain elements. Currently, only
the following attributes are supported:

- `id`
- `data-testid`
- `data-test-id`
- `data-test`

```js
// Fill an input with the id "username"
await page.fill('id=username', 'value');

// Click an element with data-test-id "submit"
await page.click('data-test-id=submit');
```

```python async
# Fill an input with the id "username"
await page.fill('id=username', 'value')

# Click an element with data-test-id "submit"
await page.click('data-test-id=submit')
```

```python sync
# Fill an input with the id "username"
page.fill('id=username', 'value')

# Click an element with data-test-id "submit"
page.click('data-test-id=submit')
```

:::note
Attribute selectors piece shadow DOM. To opt-out from this behavior, use `:light` suffix after attribute, for example `page.click('data-test-id:light=submit')
:::


## Pick n-th match from the query result

Expand Down

1 comment on commit b285936

@qabrain
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clarification and update.

Please sign in to comment.