playwright-ruby-client 1.38.1 → 1.39.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/documentation/docs/api/browser.md +13 -9
- data/documentation/docs/api/browser_context.md +51 -32
- data/documentation/docs/api/browser_type.md +12 -7
- data/documentation/docs/api/dialog.md +18 -15
- data/documentation/docs/api/element_handle.md +6 -5
- data/documentation/docs/api/frame.md +54 -24
- data/documentation/docs/api/locator.md +26 -0
- data/documentation/docs/api/page.md +101 -48
- data/documentation/docs/api/playwright.md +23 -20
- data/documentation/docs/api/selectors.md +34 -29
- data/documentation/docs/include/api_coverage.md +0 -43
- data/lib/playwright/channel.rb +9 -1
- data/lib/playwright/channel_owner.rb +7 -2
- data/lib/playwright/channel_owners/browser_context.rb +1 -1
- data/lib/playwright/channel_owners/local_utils.rb +27 -0
- data/lib/playwright/channel_owners/playwright.rb +1 -24
- data/lib/playwright/connection.rb +1 -1
- data/lib/playwright/console_message_impl.rb +29 -0
- data/lib/playwright/errors.rb +0 -2
- data/lib/playwright/javascript/value_serializer.rb +1 -1
- data/lib/playwright/locator_impl.rb +5 -24
- data/lib/playwright/version.rb +2 -2
- data/lib/playwright_api/browser.rb +2 -2
- data/lib/playwright_api/browser_context.rb +4 -4
- data/lib/playwright_api/browser_type.rb +2 -2
- data/lib/playwright_api/console_message.rb +0 -22
- data/lib/playwright_api/dialog.rb +2 -2
- data/lib/playwright_api/element_handle.rb +1 -1
- data/lib/playwright_api/frame.rb +7 -7
- data/lib/playwright_api/locator.rb +26 -5
- data/lib/playwright_api/page.rb +23 -23
- data/lib/playwright_api/playwright.rb +4 -4
- data/lib/playwright_api/selectors.rb +2 -2
- data/sig/playwright.rbs +0 -43
- metadata +4 -8
- data/documentation/docs/api/locator_assertions.md +0 -684
- data/lib/playwright/channel_owners/console_message.rb +0 -25
- data/lib/playwright/locator_assertions_impl.rb +0 -417
- data/lib/playwright/test.rb +0 -68
- data/lib/playwright_api/locator_assertions.rb +0 -538
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0dedf5c9cce96afd64ad16c45baed1b2c1fe37bb9b2632a741d8b75640bf2ae
|
4
|
+
data.tar.gz: 5f056dc5acd7bd5860c3ccaee1d7e5460923e9eb2a985c41ec1e36c8d8c91b7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82629797b234ab08abe2c9258605d0d7826eeaf3f4f70274e3f1ced8a5b41560ee1832f7ae98522ce7dfe1a826bd5058c88e9aba7f6d507d16bfa20d20206cea
|
7
|
+
data.tar.gz: '0027783e79287ba14021438154229a4de61c041c983a9f6fa582d533524df395bfb9cfaedbabf9e3d6be015a2ebed9a1fa03674bd86965f693bbd12bae9c67ce'
|
@@ -8,15 +8,19 @@ sidebar_position: 10
|
|
8
8
|
|
9
9
|
A Browser is created via [BrowserType#launch](./browser_type#launch). An example of using a [Browser](./browser) to create a [Page](./page):
|
10
10
|
|
11
|
-
```
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
11
|
+
```python sync title=example_5d31815545511b1d8ce5dfce5b153cb5ea46a1868cee95eb211d77f33026788b.py
|
12
|
+
from playwright.sync_api import sync_playwright, Playwright
|
13
|
+
|
14
|
+
def run(playwright: Playwright):
|
15
|
+
firefox = playwright.firefox
|
16
|
+
browser = firefox.launch()
|
17
|
+
page = browser.new_page()
|
18
|
+
page.goto("https://example.com")
|
19
|
+
browser.close()
|
20
|
+
|
21
|
+
with sync_playwright() as playwright:
|
22
|
+
run(playwright)
|
23
|
+
|
20
24
|
```
|
21
25
|
|
22
26
|
## browser_type
|
@@ -158,21 +158,29 @@ See [Page#expose_binding](./page#expose_binding) for page-only version.
|
|
158
158
|
|
159
159
|
An example of exposing page URL to all frames in all pages in the context:
|
160
160
|
|
161
|
-
```
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
<
|
172
|
-
|
173
|
-
|
161
|
+
```python sync title=example_a450852d36dda88564582371af8d87bb58b1a517aac4fa60b7a58a0e41c5ceff.py
|
162
|
+
from playwright.sync_api import sync_playwright, Playwright
|
163
|
+
|
164
|
+
def run(playwright: Playwright):
|
165
|
+
webkit = playwright.webkit
|
166
|
+
browser = webkit.launch(headless=false)
|
167
|
+
context = browser.new_context()
|
168
|
+
context.expose_binding("pageURL", lambda source: source["page"].url)
|
169
|
+
page = context.new_page()
|
170
|
+
page.set_content("""
|
171
|
+
<script>
|
172
|
+
async function onClick() {
|
173
|
+
document.querySelector('div').textContent = await window.pageURL();
|
174
|
+
}
|
175
|
+
</script>
|
176
|
+
<button onclick="onClick()">Click me</button>
|
177
|
+
<div></div>
|
178
|
+
""")
|
179
|
+
page.get_by_role("button").click()
|
180
|
+
|
181
|
+
with sync_playwright() as playwright:
|
182
|
+
run(playwright)
|
174
183
|
|
175
|
-
page.get_by_role("button").click
|
176
184
|
```
|
177
185
|
|
178
186
|
An example of passing an element handle:
|
@@ -217,25 +225,36 @@ See [Page#expose_function](./page#expose_function) for page-only version.
|
|
217
225
|
|
218
226
|
An example of adding a `sha256` function to all pages in the context:
|
219
227
|
|
220
|
-
```
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
228
|
+
```python sync title=example_714719de9c92e66678257180301c2512f8cd69185f53a5121b6c52194f61a871.py
|
229
|
+
import hashlib
|
230
|
+
from playwright.sync_api import sync_playwright
|
231
|
+
|
232
|
+
def sha256(text: str) -> str:
|
233
|
+
m = hashlib.sha256()
|
234
|
+
m.update(bytes(text, "utf8"))
|
235
|
+
return m.hexdigest()
|
236
|
+
|
237
|
+
|
238
|
+
def run(playwright: Playwright):
|
239
|
+
webkit = playwright.webkit
|
240
|
+
browser = webkit.launch(headless=False)
|
241
|
+
context = browser.new_context()
|
242
|
+
context.expose_function("sha256", sha256)
|
243
|
+
page = context.new_page()
|
244
|
+
page.set_content("""
|
245
|
+
<script>
|
246
|
+
async function onClick() {
|
247
|
+
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
|
248
|
+
}
|
249
|
+
</script>
|
250
|
+
<button onclick="onClick()">Click me</button>
|
251
|
+
<div></div>
|
252
|
+
""")
|
253
|
+
page.get_by_role("button").click()
|
254
|
+
|
255
|
+
with sync_playwright() as playwright:
|
256
|
+
run(playwright)
|
226
257
|
|
227
|
-
browser_context.expose_function("sha256", method(:sha256))
|
228
|
-
page = browser_context.new_page()
|
229
|
-
page.content = <<~HTML
|
230
|
-
<script>
|
231
|
-
async function onClick() {
|
232
|
-
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
|
233
|
-
}
|
234
|
-
</script>
|
235
|
-
<button onclick="onClick()">Click me</button>
|
236
|
-
<div></div>
|
237
|
-
HTML
|
238
|
-
page.get_by_role("button").click
|
239
258
|
```
|
240
259
|
|
241
260
|
## grant_permissions
|
@@ -8,15 +8,20 @@ sidebar_position: 10
|
|
8
8
|
BrowserType provides methods to launch a specific browser instance or connect to an existing one. The following is a
|
9
9
|
typical example of using Playwright to drive automation:
|
10
10
|
|
11
|
-
```
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
```python sync title=example_2f9fbff87f35af4b76a27f54efeca3201696bbfa94ce03fee5a3df2639cc27d3.py
|
12
|
+
from playwright.sync_api import sync_playwright, Playwright
|
13
|
+
|
14
|
+
def run(playwright: Playwright):
|
15
|
+
chromium = playwright.chromium
|
16
|
+
browser = chromium.launch()
|
17
|
+
page = browser.new_page()
|
18
|
+
page.goto("https://example.com")
|
19
|
+
# other actions...
|
20
|
+
browser.close()
|
16
21
|
|
17
|
-
|
22
|
+
with sync_playwright() as playwright:
|
23
|
+
run(playwright)
|
18
24
|
|
19
|
-
end
|
20
25
|
```
|
21
26
|
|
22
27
|
## connect_over_cdp
|
@@ -9,21 +9,24 @@ sidebar_position: 10
|
|
9
9
|
|
10
10
|
An example of using [Dialog](./dialog) class:
|
11
11
|
|
12
|
-
```
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
dialog.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
page.
|
23
|
-
page.
|
24
|
-
|
25
|
-
|
26
|
-
|
12
|
+
```python sync title=example_a7dcc75b7aa5544237ac3a964e9196d0445308864d3ce820f8cb8396f687b04a.py
|
13
|
+
from playwright.sync_api import sync_playwright, Playwright
|
14
|
+
|
15
|
+
def handle_dialog(dialog):
|
16
|
+
print(dialog.message)
|
17
|
+
dialog.dismiss()
|
18
|
+
|
19
|
+
def run(playwright: Playwright):
|
20
|
+
chromium = playwright.chromium
|
21
|
+
browser = chromium.launch()
|
22
|
+
page = browser.new_page()
|
23
|
+
page.on("dialog", handle_dialog)
|
24
|
+
page.evaluate("alert('1')")
|
25
|
+
browser.close()
|
26
|
+
|
27
|
+
with sync_playwright() as playwright:
|
28
|
+
run(playwright)
|
29
|
+
|
27
30
|
```
|
28
31
|
|
29
32
|
**NOTE**: Dialogs are dismissed automatically, unless there is a [`event: Page.dialog`] listener.
|
@@ -505,13 +505,14 @@ Triggers a `change` and `input` event once all the provided options have been se
|
|
505
505
|
|
506
506
|
**Usage**
|
507
507
|
|
508
|
-
```
|
509
|
-
#
|
510
|
-
|
508
|
+
```python sync title=example_e6bbc99e34c9f6ee73aa7d4265d34af456e6c67d185530f0a77f8064050a3ec4.py
|
509
|
+
# Single selection matching the value or label
|
510
|
+
handle.select_option("blue")
|
511
511
|
# single selection matching both the label
|
512
|
-
|
512
|
+
handle.select_option(label="blue")
|
513
513
|
# multiple selection
|
514
|
-
|
514
|
+
handle.select_option(value=["red", "green", "blue"])
|
515
|
+
|
515
516
|
```
|
516
517
|
|
517
518
|
## select_text
|
@@ -15,16 +15,25 @@ At every point of time, page exposes its current frame tree via the [Page#main_f
|
|
15
15
|
|
16
16
|
An example of dumping frame tree:
|
17
17
|
|
18
|
-
```
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
```python sync title=example_2bc8a0187190738d8dc7b29c66ad5f9f2187fd1827455e9ceb1e9ace26aaf534.py
|
19
|
+
from playwright.sync_api import sync_playwright, Playwright
|
20
|
+
|
21
|
+
def run(playwright: Playwright):
|
22
|
+
firefox = playwright.firefox
|
23
|
+
browser = firefox.launch()
|
24
|
+
page = browser.new_page()
|
25
|
+
page.goto("https://www.theverge.com")
|
26
|
+
dump_frame_tree(page.main_frame, "")
|
27
|
+
browser.close()
|
28
|
+
|
29
|
+
def dump_frame_tree(frame, indent):
|
30
|
+
print(indent + frame.name + '@' + frame.url)
|
31
|
+
for child in frame.child_frames:
|
32
|
+
dump_frame_tree(child, indent + " ")
|
33
|
+
|
34
|
+
with sync_playwright() as playwright:
|
35
|
+
run(playwright)
|
25
36
|
|
26
|
-
page.goto("https://www.theverge.com")
|
27
|
-
dump_frame_tree(page.main_frame)
|
28
37
|
```
|
29
38
|
|
30
39
|
## add_script_tag
|
@@ -911,13 +920,14 @@ Triggers a `change` and `input` event once all the provided options have been se
|
|
911
920
|
|
912
921
|
**Usage**
|
913
922
|
|
914
|
-
```
|
915
|
-
#
|
916
|
-
frame.select_option("select#colors",
|
923
|
+
```python sync title=example_3f390f340c78c42dd0c88a09b2f56575b02b163786e8cdee33581217afced6b2.py
|
924
|
+
# Single selection matching the value or label
|
925
|
+
frame.select_option("select#colors", "blue")
|
917
926
|
# single selection matching both the label
|
918
|
-
frame.select_option("select#colors", label
|
927
|
+
frame.select_option("select#colors", label="blue")
|
919
928
|
# multiple selection
|
920
|
-
frame.select_option("select#colors", value
|
929
|
+
frame.select_option("select#colors", value=["red", "green", "blue"])
|
930
|
+
|
921
931
|
```
|
922
932
|
|
923
933
|
## set_checked
|
@@ -1089,9 +1099,20 @@ Returns when the `expression` returns a truthy value, returns that value.
|
|
1089
1099
|
|
1090
1100
|
The [Frame#wait_for_function](./frame#wait_for_function) can be used to observe viewport size change:
|
1091
1101
|
|
1092
|
-
```
|
1093
|
-
|
1094
|
-
|
1102
|
+
```python sync title=example_e6a8c279eb09e58e3522cb6237f5d62165b164cad0c1916720af299ffcb8dc8a.py
|
1103
|
+
from playwright.sync_api import sync_playwright, Playwright
|
1104
|
+
|
1105
|
+
def run(playwright: Playwright):
|
1106
|
+
webkit = playwright.webkit
|
1107
|
+
browser = webkit.launch()
|
1108
|
+
page = browser.new_page()
|
1109
|
+
page.evaluate("window.x = 0; setTimeout(() => { window.x = 100 }, 1000);")
|
1110
|
+
page.main_frame.wait_for_function("() => window.x > 0")
|
1111
|
+
browser.close()
|
1112
|
+
|
1113
|
+
with sync_playwright() as playwright:
|
1114
|
+
run(playwright)
|
1115
|
+
|
1095
1116
|
```
|
1096
1117
|
|
1097
1118
|
To pass an argument to the predicate of `frame.waitForFunction` function:
|
@@ -1167,13 +1188,22 @@ function will throw.
|
|
1167
1188
|
|
1168
1189
|
This method works across navigations:
|
1169
1190
|
|
1170
|
-
```
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1191
|
+
```python sync title=example_6e2a71807566cf008382d4c163ff6e71e34d7f10ef6706ad7fcaa9b70c256a66.py
|
1192
|
+
from playwright.sync_api import sync_playwright, Playwright
|
1193
|
+
|
1194
|
+
def run(playwright: Playwright):
|
1195
|
+
chromium = playwright.chromium
|
1196
|
+
browser = chromium.launch()
|
1197
|
+
page = browser.new_page()
|
1198
|
+
for current_url in ["https://google.com", "https://bbc.com"]:
|
1199
|
+
page.goto(current_url, wait_until="domcontentloaded")
|
1200
|
+
element = page.main_frame.wait_for_selector("img")
|
1201
|
+
print("Loaded image: " + str(element.get_attribute("src")))
|
1202
|
+
browser.close()
|
1203
|
+
|
1204
|
+
with sync_playwright() as playwright:
|
1205
|
+
run(playwright)
|
1206
|
+
|
1177
1207
|
```
|
1178
1208
|
|
1179
1209
|
## wait_for_timeout
|
@@ -42,6 +42,8 @@ def all_inner_texts
|
|
42
42
|
|
43
43
|
Returns an array of `node.innerText` values for all matching nodes.
|
44
44
|
|
45
|
+
**NOTE**: If you need to assert text on the page, prefer [`method: LocatorAssertions.toHaveText`] with `useInnerText` option to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
46
|
+
|
45
47
|
**Usage**
|
46
48
|
|
47
49
|
```ruby
|
@@ -57,6 +59,8 @@ def all_text_contents
|
|
57
59
|
|
58
60
|
Returns an array of `node.textContent` values for all matching nodes.
|
59
61
|
|
62
|
+
**NOTE**: If you need to assert text on the page, prefer [`method: LocatorAssertions.toHaveText`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
63
|
+
|
60
64
|
**Usage**
|
61
65
|
|
62
66
|
```ruby
|
@@ -232,6 +236,8 @@ def count
|
|
232
236
|
|
233
237
|
Returns the number of elements matching the locator.
|
234
238
|
|
239
|
+
**NOTE**: If you need to assert the number of elements on the page, prefer [`method: LocatorAssertions.toHaveCount`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
240
|
+
|
235
241
|
**Usage**
|
236
242
|
|
237
243
|
```ruby
|
@@ -526,6 +532,8 @@ alias: `[]`
|
|
526
532
|
|
527
533
|
Returns the matching element's attribute value.
|
528
534
|
|
535
|
+
**NOTE**: If you need to assert an element's attribute, prefer [`method: LocatorAssertions.toHaveAttribute`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
536
|
+
|
529
537
|
## get_by_alt_text
|
530
538
|
|
531
539
|
```
|
@@ -808,6 +816,8 @@ def inner_text(timeout: nil)
|
|
808
816
|
|
809
817
|
Returns the [`element.innerText`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText).
|
810
818
|
|
819
|
+
**NOTE**: If you need to assert text on the page, prefer [`method: LocatorAssertions.toHaveText`] with `useInnerText` option to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
820
|
+
|
811
821
|
## input_value
|
812
822
|
|
813
823
|
```
|
@@ -817,6 +827,8 @@ def input_value(timeout: nil)
|
|
817
827
|
|
818
828
|
Returns the value for the matching `<input>` or `<textarea>` or `<select>` element.
|
819
829
|
|
830
|
+
**NOTE**: If you need to assert input value, prefer [`method: LocatorAssertions.toHaveValue`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
831
|
+
|
820
832
|
**Usage**
|
821
833
|
|
822
834
|
```ruby
|
@@ -836,6 +848,8 @@ def checked?(timeout: nil)
|
|
836
848
|
|
837
849
|
Returns whether the element is checked. Throws if the element is not a checkbox or radio input.
|
838
850
|
|
851
|
+
**NOTE**: If you need to assert that checkbox is checked, prefer [`method: LocatorAssertions.toBeChecked`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
852
|
+
|
839
853
|
**Usage**
|
840
854
|
|
841
855
|
```ruby
|
@@ -851,6 +865,8 @@ def disabled?(timeout: nil)
|
|
851
865
|
|
852
866
|
Returns whether the element is disabled, the opposite of [enabled](https://playwright.dev/python/docs/actionability#enabled).
|
853
867
|
|
868
|
+
**NOTE**: If you need to assert that an element is disabled, prefer [`method: LocatorAssertions.toBeDisabled`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
869
|
+
|
854
870
|
**Usage**
|
855
871
|
|
856
872
|
```ruby
|
@@ -866,6 +882,8 @@ def editable?(timeout: nil)
|
|
866
882
|
|
867
883
|
Returns whether the element is [editable](https://playwright.dev/python/docs/actionability#editable).
|
868
884
|
|
885
|
+
**NOTE**: If you need to assert that an element is editable, prefer [`method: LocatorAssertions.toBeEditable`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
886
|
+
|
869
887
|
**Usage**
|
870
888
|
|
871
889
|
```ruby
|
@@ -881,6 +899,8 @@ def enabled?(timeout: nil)
|
|
881
899
|
|
882
900
|
Returns whether the element is [enabled](https://playwright.dev/python/docs/actionability#enabled).
|
883
901
|
|
902
|
+
**NOTE**: If you need to assert that an element is enabled, prefer [`method: LocatorAssertions.toBeEnabled`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
903
|
+
|
884
904
|
**Usage**
|
885
905
|
|
886
906
|
```ruby
|
@@ -896,6 +916,8 @@ def hidden?(timeout: nil)
|
|
896
916
|
|
897
917
|
Returns whether the element is hidden, the opposite of [visible](https://playwright.dev/python/docs/actionability#visible).
|
898
918
|
|
919
|
+
**NOTE**: If you need to assert that element is hidden, prefer [`method: LocatorAssertions.toBeHidden`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
920
|
+
|
899
921
|
**Usage**
|
900
922
|
|
901
923
|
```ruby
|
@@ -911,6 +933,8 @@ def visible?(timeout: nil)
|
|
911
933
|
|
912
934
|
Returns whether the element is [visible](https://playwright.dev/python/docs/actionability#visible).
|
913
935
|
|
936
|
+
**NOTE**: If you need to assert that element is visible, prefer [`method: LocatorAssertions.toBeVisible`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
937
|
+
|
914
938
|
**Usage**
|
915
939
|
|
916
940
|
```ruby
|
@@ -1277,6 +1301,8 @@ def text_content(timeout: nil)
|
|
1277
1301
|
|
1278
1302
|
Returns the [`node.textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent).
|
1279
1303
|
|
1304
|
+
**NOTE**: If you need to assert text on the page, prefer [`method: LocatorAssertions.toHaveText`] to avoid flakiness. See [assertions guide](https://playwright.dev/python/docs/test-assertions) for more details.
|
1305
|
+
|
1280
1306
|
## type
|
1281
1307
|
|
1282
1308
|
```
|
@@ -12,12 +12,21 @@ instance might have multiple [Page](./page) instances.
|
|
12
12
|
|
13
13
|
This example creates a page, navigates it to a URL, and then saves a screenshot:
|
14
14
|
|
15
|
-
```
|
16
|
-
playwright.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
```python sync title=example_94e620cdbdfd41e2c9b14d561052ffa89535fc346038c4584ea4dd8520f5401c.py
|
16
|
+
from playwright.sync_api import sync_playwright, Playwright
|
17
|
+
|
18
|
+
def run(playwright: Playwright):
|
19
|
+
webkit = playwright.webkit
|
20
|
+
browser = webkit.launch()
|
21
|
+
context = browser.new_context()
|
22
|
+
page = context.new_page()
|
23
|
+
page.goto("https://example.com")
|
24
|
+
page.screenshot(path="screenshot.png")
|
25
|
+
browser.close()
|
26
|
+
|
27
|
+
with sync_playwright() as playwright:
|
28
|
+
run(playwright)
|
29
|
+
|
21
30
|
```
|
22
31
|
|
23
32
|
The Page class emits various events (described below) which can be handled using any of Node's native
|
@@ -462,18 +471,29 @@ See [BrowserContext#expose_binding](./browser_context#expose_binding) for the co
|
|
462
471
|
|
463
472
|
An example of exposing page URL to all frames in a page:
|
464
473
|
|
465
|
-
```
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
474
|
+
```python sync title=example_4f7d99a72aaea957cc5678ed8728965338d78598d7772f47fbf23c28f0eba52d.py
|
475
|
+
from playwright.sync_api import sync_playwright, Playwright
|
476
|
+
|
477
|
+
def run(playwright: Playwright):
|
478
|
+
webkit = playwright.webkit
|
479
|
+
browser = webkit.launch(headless=false)
|
480
|
+
context = browser.new_context()
|
481
|
+
page = context.new_page()
|
482
|
+
page.expose_binding("pageURL", lambda source: source["page"].url)
|
483
|
+
page.set_content("""
|
484
|
+
<script>
|
485
|
+
async function onClick() {
|
486
|
+
document.querySelector('div').textContent = await window.pageURL();
|
487
|
+
}
|
488
|
+
</script>
|
489
|
+
<button onclick="onClick()">Click me</button>
|
490
|
+
<div></div>
|
491
|
+
""")
|
492
|
+
page.click("button")
|
493
|
+
|
494
|
+
with sync_playwright() as playwright:
|
495
|
+
run(playwright)
|
496
|
+
|
477
497
|
```
|
478
498
|
|
479
499
|
An example of passing an element handle:
|
@@ -517,24 +537,35 @@ See [BrowserContext#expose_function](./browser_context#expose_function) for cont
|
|
517
537
|
|
518
538
|
An example of adding a `sha256` function to the page:
|
519
539
|
|
520
|
-
```
|
521
|
-
|
540
|
+
```python sync title=example_0f68a39bdff02a3df161c74e81cabb8a2ff1f09f0d09f6ef9b799a6f2f19a280.py
|
541
|
+
import hashlib
|
542
|
+
from playwright.sync_api import sync_playwright, Playwright
|
522
543
|
|
523
|
-
def
|
524
|
-
|
525
|
-
|
544
|
+
def sha256(text):
|
545
|
+
m = hashlib.sha256()
|
546
|
+
m.update(bytes(text, "utf8"))
|
547
|
+
return m.hexdigest()
|
548
|
+
|
549
|
+
|
550
|
+
def run(playwright: Playwright):
|
551
|
+
webkit = playwright.webkit
|
552
|
+
browser = webkit.launch(headless=False)
|
553
|
+
page = browser.new_page()
|
554
|
+
page.expose_function("sha256", sha256)
|
555
|
+
page.set_content("""
|
556
|
+
<script>
|
557
|
+
async function onClick() {
|
558
|
+
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
|
559
|
+
}
|
560
|
+
</script>
|
561
|
+
<button onclick="onClick()">Click me</button>
|
562
|
+
<div></div>
|
563
|
+
""")
|
564
|
+
page.click("button")
|
565
|
+
|
566
|
+
with sync_playwright() as playwright:
|
567
|
+
run(playwright)
|
526
568
|
|
527
|
-
page.expose_function("sha256", method(:sha256))
|
528
|
-
page.content = <<~HTML
|
529
|
-
<script>
|
530
|
-
async function onClick() {
|
531
|
-
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
|
532
|
-
}
|
533
|
-
</script>
|
534
|
-
<button onclick="onClick()">Click me</button>
|
535
|
-
<div></div>
|
536
|
-
HTML
|
537
|
-
page.locator("button").click
|
538
569
|
```
|
539
570
|
|
540
571
|
## fill
|
@@ -1324,13 +1355,14 @@ Triggers a `change` and `input` event once all the provided options have been se
|
|
1324
1355
|
|
1325
1356
|
**Usage**
|
1326
1357
|
|
1327
|
-
```
|
1328
|
-
#
|
1329
|
-
page.select_option("select#colors",
|
1358
|
+
```python sync title=example_8260034c740933903e5a39d30a4f4e388bdffa9e82acd9a5fe1fb774752a505a.py
|
1359
|
+
# Single selection matching the value or label
|
1360
|
+
page.select_option("select#colors", "blue")
|
1330
1361
|
# single selection matching both the label
|
1331
|
-
page.select_option("select#colors", label
|
1362
|
+
page.select_option("select#colors", label="blue")
|
1332
1363
|
# multiple selection
|
1333
|
-
page.select_option("select#colors", value
|
1364
|
+
page.select_option("select#colors", value=["red", "green", "blue"])
|
1365
|
+
|
1334
1366
|
```
|
1335
1367
|
|
1336
1368
|
## set_checked
|
@@ -1644,9 +1676,20 @@ Returns when the `expression` returns a truthy value. It resolves to a JSHandle
|
|
1644
1676
|
|
1645
1677
|
The [Page#wait_for_function](./page#wait_for_function) can be used to observe viewport size change:
|
1646
1678
|
|
1647
|
-
```
|
1648
|
-
|
1649
|
-
|
1679
|
+
```python sync title=example_83eed1f1f00ad73f641bf4a49f672e81c4faf1ca098a4a5070afeeabb88312f5.py
|
1680
|
+
from playwright.sync_api import sync_playwright, Playwright
|
1681
|
+
|
1682
|
+
def run(playwright: Playwright):
|
1683
|
+
webkit = playwright.webkit
|
1684
|
+
browser = webkit.launch()
|
1685
|
+
page = browser.new_page()
|
1686
|
+
page.evaluate("window.x = 0; setTimeout(() => { window.x = 100 }, 1000);")
|
1687
|
+
page.wait_for_function("() => window.x > 0")
|
1688
|
+
browser.close()
|
1689
|
+
|
1690
|
+
with sync_playwright() as playwright:
|
1691
|
+
run(playwright)
|
1692
|
+
|
1650
1693
|
```
|
1651
1694
|
|
1652
1695
|
To pass an argument to the predicate of [Page#wait_for_function](./page#wait_for_function) function:
|
@@ -1814,12 +1857,22 @@ function will throw.
|
|
1814
1857
|
|
1815
1858
|
This method works across navigations:
|
1816
1859
|
|
1817
|
-
```
|
1818
|
-
|
1819
|
-
|
1820
|
-
|
1821
|
-
|
1822
|
-
|
1860
|
+
```python sync title=example_903c7325fd65fcdf6f22c77fc159922a568841abce60ae1b7c54ab5837401862.py
|
1861
|
+
from playwright.sync_api import sync_playwright, Playwright
|
1862
|
+
|
1863
|
+
def run(playwright: Playwright):
|
1864
|
+
chromium = playwright.chromium
|
1865
|
+
browser = chromium.launch()
|
1866
|
+
page = browser.new_page()
|
1867
|
+
for current_url in ["https://google.com", "https://bbc.com"]:
|
1868
|
+
page.goto(current_url, wait_until="domcontentloaded")
|
1869
|
+
element = page.wait_for_selector("img")
|
1870
|
+
print("Loaded image: " + str(element.get_attribute("src")))
|
1871
|
+
browser.close()
|
1872
|
+
|
1873
|
+
with sync_playwright() as playwright:
|
1874
|
+
run(playwright)
|
1875
|
+
|
1823
1876
|
```
|
1824
1877
|
|
1825
1878
|
## wait_for_timeout
|