You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs-src/api-footer.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,6 +84,12 @@ Selector describes an element in the page. It can be used to obtain `ElementHand
84
84
85
85
Selector has the following format: `engine=body [>> engine=body]*`. Here `engine` is one of the supported [selector engines](selectors.md) (e.g. `css` or `xpath`), and `body` is a selector body in the format of the particular engine. When multiple `engine=body` clauses are present (separated by `>>`), next one is queried relative to the previous one's result.
86
86
87
+
Playwright also supports the following CSS extensions:
88
+
*`:text("string")` - Matches elements that contain specific text node. Learn more about [text selector](./selectors.md#css-extension-text).
89
+
*`:visible` - Matches only visible elements. Learn more about [visible selector](./selectors.md#css-extension-visible).
90
+
*`:light(selector)` - Matches in the light DOM only as opposite to piercing open shadow roots. Learn more about [shadow piercing](./selectors.md#shadow-piercing).
91
+
*`:right-of(selector)`, `:left-of(selector)`, `:above(selector)`, `:below(selector)`, `:near(selector)`, `:within(selector)` - Match elements based on their relative position to another element. Learn more about [proximity selectors](./selectors.md#css-extension-proximity).
92
+
87
93
For convenience, selectors in the wrong format are heuristically converted to the right format:
88
94
- selector starting with `//` or `..` is assumed to be `xpath=selector`;
89
95
- selector starting and ending with a quote (either `"` or `'`) is assumed to be `text=selector`;
Copy file name to clipboardExpand all lines: docs/api.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5456,6 +5456,12 @@ Selector describes an element in the page. It can be used to obtain `ElementHand
5456
5456
5457
5457
Selector has the following format: `engine=body [>> engine=body]*`. Here `engine` is one of the supported [selector engines](selectors.md) (e.g. `css` or `xpath`), and `body` is a selector body in the format of the particular engine. When multiple `engine=body` clauses are present (separated by `>>`), next one is queried relative to the previous one's result.
5458
5458
5459
+
Playwright also supports the following CSS extensions:
5460
+
*`:text("string")` - Matches elements that contain specific text node. Learn more about [text selector](./selectors.md#css-extension-text).
5461
+
*`:visible` - Matches only visible elements. Learn more about [visible selector](./selectors.md#css-extension-visible).
5462
+
*`:light(selector)` - Matches in the light DOM only as opposite to piercing open shadow roots. Learn more about [shadow piercing](./selectors.md#shadow-piercing).
5463
+
*`:right-of(selector)`, `:left-of(selector)`, `:above(selector)`, `:below(selector)`, `:near(selector)`, `:within(selector)` - Match elements based on their relative position to another element. Learn more about [proximity selectors](./selectors.md#css-extension-proximity).
5464
+
5459
5465
For convenience, selectors in the wrong format are heuristically converted to the right format:
5460
5466
- selector starting with `//` or `..` is assumed to be `xpath=selector`;
5461
5467
- selector starting and ending with a quote (either `"` or `'`) is assumed to be `text=selector`;
`css` is a default engine - any malformed selector not starting with `//` nor starting and ending with a quote is assumed to be a css selector. For example, Playwright converts `page.$('span > button')` to `page.$('css=span > button')`.
138
138
139
-
`css:light` engine is equivalent to [`Document.querySelector`](https://developer.mozilla.org/en/docs/Web/API/Document/querySelector) and behaves according to the CSS spec. However, it does not pierce shadow roots, which may be inconvenient when working with [Shadow DOM and Web Components](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM). For that reason, `css` engine pierces shadow roots. More specifically, every [Descendant combinator](https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_combinator) pierces an arbitrary number of open shadow roots, including the implicit descendant combinator at the start of the selector.
139
+
Playwright augments standard CSS selectors in two ways, see below for more details:
140
+
*`css` engine pierces open shadow DOM by default.
141
+
* Playwright adds a few custom pseudo-classes like `:visible`.
140
142
141
-
`css` engine first searches for elements in the light dom in the iteration order, and then recursively inside open shadow roots in the iteration order. It does not search inside closed shadow roots or iframes.
143
+
#### Shadow piercing
144
+
145
+
`css:light` engine is equivalent to [`Document.querySelector`](https://developer.mozilla.org/en/docs/Web/API/Document/querySelector) and behaves according to the CSS spec. However, it does not pierce shadow roots, which may be inconvenient when working with [Shadow DOM and Web Components](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM). For that reason, `css` engine pierces shadow roots. More specifically, any [Descendant combinator](https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_combinator) or [Child combinator](https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator) pierces an arbitrary number of open shadow roots, including the implicit descendant combinator at the start of the selector.
142
146
143
-
#### Examples
147
+
`css` engine first searches for elements in the light dom in the iteration order, and then recursively inside open shadow roots in the iteration order. It does not search inside closed shadow roots or iframes.
144
148
145
149
```html
146
150
<article>
@@ -171,6 +175,68 @@ Note that `<open mode shadow root>` is not an html element, but rather a shadow
171
175
-`"css:light=article > .in-the-shadow"` does not match anything.
172
176
-`"css=article li#target"` matches the `<li id='target'>Deep in the shadow</li>`, piercing two shadow roots.
173
177
178
+
#### CSS extension: visible
179
+
180
+
The `:visible` pseudo-class matches elements that are visible as defined in the [actionability](./actionability.md#visible) guide. For example, `input` matches all the inputs on the page, while `input:visible` matches only visible inputs. This is useful to distinguish elements that are very similar but differ in visibility.
181
+
182
+
```js
183
+
// Clicks the first button.
184
+
awaitpage.click('button');
185
+
// Clicks the first visible button. If there are some invisible buttons, this click will just ignore them.
186
+
awaitpage.click('button:visible');
187
+
```
188
+
189
+
Use `:visible` with caution, because it has two major drawbacks:
190
+
* When elements change their visibility dynamically, `:visible` will give upredictable results based on the timing.
191
+
*`:visible` forces a layout and may lead to querying being slow, especially when used with `page.waitForSelector(selector[, options])` method.
192
+
193
+
#### CSS extension: text
194
+
195
+
The `:text` pseudo-class matches elements that have a text node child with specific text. It is similar to the [text engine](#text-and-textlight). There are a few variations that support different arguments:
196
+
197
+
*`:text("exact match")` - Only matches when element's text exactly equals to passed string.
198
+
*`:text("substring", "g")` - Matches when element's text contains "substring" somewhere.
*`:text("string with spaces", "s")` - Normalizes whitespace when matching, for example turns multiple spaces into one and line breaks into spaces.
201
+
*`:text("substring", "sgi")` - Different flags may be combined. For example, pass `"sgi"` to match by case-insensitive substring with normalized whitespace.
202
+
*`button:text("Sign in")` - Text selector may be combined with regular CSS.
203
+
*`:matches-text("[+-]?\\d+")` - Matches text against a regular expression. Note that back-slash `\` and quotes `"` must be escaped.
204
+
*`:matches-text("regex", "g")` - Matches text against a regular expression with specified flags. Learn more about [regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp).
205
+
206
+
```js
207
+
// Click a button with text "Sign in".
208
+
awaitpage.click('button:text("Sign in")');
209
+
```
210
+
211
+
#### CSS extension: light
212
+
213
+
`css` engine [pierces shadow](#shadow-piercing) by default. It is possible to disable this behavior by wrapping a selector in `:light` pseudo-class: `:light(section > button.class)` matches in light DOM only.
214
+
215
+
```js
216
+
awaitpage.click(':light(.article > .header)');
217
+
```
218
+
219
+
#### CSS extension: proximity
220
+
221
+
Playwright provides a few proximity selectors based on the page layout. These can be combined with regular CSS for better results, for example `input:right-of(:text("Password"))` matches an input field that is to the right of text "Password".
222
+
223
+
Note that Playwright uses some heuristics to determine whether one element should be considered to the left/right/above/below/near/within another. Therefore, using proximity selectors may produce unpredictable results. For example, selector could stop matching when element moves by one pixel.
224
+
225
+
*`:right-of(css > selector)` - Matches elements that are to the right of any element matching the inner selector.
226
+
*`:left-of(css > selector)` - Matches elements that are to the left of any element matching the inner selector.
227
+
*`:above(css > selector)` - Matches elements that are above any of the elements matching the inner selector.
228
+
*`:below(css > selector)` - Matches elements that are below any of the elements matching the inner selector.
229
+
*`:near(css > selector)` - Matches elements that are near any of the elements matching the inner selector.
230
+
*`:within(css > selector)` - Matches elements that are within any of the elements matching the inner selector.
0 commit comments