playwright-ruby-client 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/documentation/README.md +33 -0
- data/documentation/babel.config.js +3 -0
- data/documentation/docs/api/accessibility.md +7 -0
- data/documentation/docs/api/browser.md +185 -0
- data/documentation/docs/api/browser_context.md +398 -0
- data/documentation/docs/api/browser_type.md +105 -0
- data/documentation/docs/api/cdp_session.md +7 -0
- data/documentation/docs/api/console_message.md +41 -0
- data/documentation/docs/api/dialog.md +74 -0
- data/documentation/docs/api/element_handle.md +640 -0
- data/documentation/docs/api/experimental/_category_.yml +3 -0
- data/documentation/docs/api/experimental/android.md +25 -0
- data/documentation/docs/api/experimental/android_device.md +91 -0
- data/documentation/docs/api/experimental/android_input.md +38 -0
- data/documentation/docs/api/experimental/android_socket.md +7 -0
- data/documentation/docs/api/experimental/android_web_view.md +7 -0
- data/documentation/docs/api/file_chooser.md +51 -0
- data/documentation/docs/api/frame.md +867 -0
- data/documentation/docs/api/js_handle.md +116 -0
- data/documentation/docs/api/keyboard.md +157 -0
- data/documentation/docs/api/mouse.md +69 -0
- data/documentation/docs/api/page.md +1469 -0
- data/documentation/docs/api/playwright.md +63 -0
- data/documentation/docs/api/request.md +188 -0
- data/documentation/docs/api/response.md +97 -0
- data/documentation/docs/api/route.md +80 -0
- data/documentation/docs/api/selectors.md +23 -0
- data/documentation/docs/api/touchscreen.md +8 -0
- data/documentation/docs/api/tracing.md +54 -0
- data/documentation/docs/api/web_socket.md +7 -0
- data/documentation/docs/api/worker.md +7 -0
- data/documentation/docs/article/api_coverage.mdx +11 -0
- data/documentation/docs/article/getting_started.md +152 -0
- data/documentation/docs/article/guides/_category_.yml +3 -0
- data/documentation/docs/article/guides/download_playwright_driver.md +49 -0
- data/documentation/docs/article/guides/launch_browser.md +119 -0
- data/documentation/docs/article/guides/rails_integration.md +51 -0
- data/{docs → documentation/docs/include}/api_coverage.md +0 -0
- data/documentation/docusaurus.config.js +107 -0
- data/documentation/package.json +39 -0
- data/documentation/sidebars.js +15 -0
- data/documentation/src/components/HomepageFeatures.js +61 -0
- data/documentation/src/components/HomepageFeatures.module.css +13 -0
- data/documentation/src/css/custom.css +44 -0
- data/documentation/src/pages/index.js +50 -0
- data/documentation/src/pages/index.module.css +41 -0
- data/documentation/src/pages/markdown-page.md +7 -0
- data/documentation/static/.nojekyll +0 -0
- data/documentation/static/img/playwright-logo.svg +9 -0
- data/documentation/static/img/undraw_dropdown_menu.svg +1 -0
- data/documentation/static/img/undraw_web_development.svg +1 -0
- data/documentation/static/img/undraw_windows.svg +1 -0
- data/documentation/yarn.lock +8805 -0
- data/lib/playwright/channel_owners/binding_call.rb +33 -0
- data/lib/playwright/channel_owners/browser_context.rb +2 -2
- data/lib/playwright/channel_owners/element_handle.rb +2 -10
- data/lib/playwright/channel_owners/frame.rb +6 -28
- data/lib/playwright/channel_owners/js_handle.rb +2 -10
- data/lib/playwright/channel_owners/page.rb +10 -1
- data/lib/playwright/input_files.rb +0 -8
- data/lib/playwright/javascript.rb +0 -10
- data/lib/playwright/javascript/expression.rb +2 -7
- data/lib/playwright/version.rb +1 -1
- data/lib/playwright_api/accessibility.rb +7 -89
- data/lib/playwright_api/android.rb +7 -64
- data/lib/playwright_api/android_device.rb +8 -8
- data/lib/playwright_api/browser.rb +15 -169
- data/lib/playwright_api/browser_context.rb +47 -609
- data/lib/playwright_api/browser_type.rb +13 -103
- data/lib/playwright_api/cdp_session.rb +2 -25
- data/lib/playwright_api/console_message.rb +6 -6
- data/lib/playwright_api/dialog.rb +11 -92
- data/lib/playwright_api/element_handle.rb +60 -362
- data/lib/playwright_api/file_chooser.rb +0 -28
- data/lib/playwright_api/frame.rb +74 -713
- data/lib/playwright_api/js_handle.rb +16 -90
- data/lib/playwright_api/keyboard.rb +21 -213
- data/lib/playwright_api/mouse.rb +1 -45
- data/lib/playwright_api/page.rb +155 -1635
- data/lib/playwright_api/playwright.rb +14 -117
- data/lib/playwright_api/request.rb +15 -121
- data/lib/playwright_api/response.rb +9 -9
- data/lib/playwright_api/route.rb +8 -105
- data/lib/playwright_api/selectors.rb +6 -97
- data/lib/playwright_api/tracing.rb +1 -61
- data/lib/playwright_api/web_socket.rb +1 -1
- data/lib/playwright_api/worker.rb +6 -42
- metadata +55 -4
- data/lib/playwright/javascript/function.rb +0 -67
@@ -0,0 +1,116 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# JSHandle
|
6
|
+
|
7
|
+
JSHandle represents an in-page JavaScript object. JSHandles can be created with the [Page#evaluate_handle](./page#evaluate_handle)
|
8
|
+
method.
|
9
|
+
|
10
|
+
```python sync title=example_c408a96b8ac9c9bd54d915009c8b477eb75b7bf9e879fd76b32f3d4b6340a667.py
|
11
|
+
window_handle = page.evaluate_handle("window")
|
12
|
+
# ...
|
13
|
+
|
14
|
+
```
|
15
|
+
|
16
|
+
JSHandle prevents the referenced JavaScript object being garbage collected unless the handle is exposed with
|
17
|
+
[JSHandle#dispose](./js_handle#dispose). JSHandles are auto-disposed when their origin frame gets navigated or the parent context
|
18
|
+
gets destroyed.
|
19
|
+
|
20
|
+
JSHandle instances can be used as an argument in [Page#eval_on_selector](./page#eval_on_selector), [Page#evaluate](./page#evaluate) and
|
21
|
+
[Page#evaluate_handle](./page#evaluate_handle) methods.
|
22
|
+
|
23
|
+
## as_element
|
24
|
+
|
25
|
+
```
|
26
|
+
def as_element
|
27
|
+
```
|
28
|
+
|
29
|
+
Returns either `null` or the object handle itself, if the object handle is an instance of [ElementHandle](./element_handle).
|
30
|
+
|
31
|
+
## dispose
|
32
|
+
|
33
|
+
```
|
34
|
+
def dispose
|
35
|
+
```
|
36
|
+
|
37
|
+
The `jsHandle.dispose` method stops referencing the element handle.
|
38
|
+
|
39
|
+
## evaluate
|
40
|
+
|
41
|
+
```
|
42
|
+
def evaluate(expression, arg: nil)
|
43
|
+
```
|
44
|
+
|
45
|
+
Returns the return value of `expression`.
|
46
|
+
|
47
|
+
This method passes this handle as the first argument to `expression`.
|
48
|
+
|
49
|
+
If `expression` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), then `handle.evaluate` would wait for the promise to resolve and return its value.
|
50
|
+
|
51
|
+
Examples:
|
52
|
+
|
53
|
+
```python sync title=example_2400f96eaaed3bc6ef6b0a16ba48e83d38a166c7d55a5dba0025472cffc6f2be.py
|
54
|
+
tweet_handle = page.query_selector(".tweet .retweets")
|
55
|
+
assert tweet_handle.evaluate("node => node.innerText") == "10 retweets"
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
## evaluate_handle
|
62
|
+
|
63
|
+
```
|
64
|
+
def evaluate_handle(expression, arg: nil)
|
65
|
+
```
|
66
|
+
|
67
|
+
Returns the return value of `expression` as a [JSHandle](./js_handle).
|
68
|
+
|
69
|
+
This method passes this handle as the first argument to `expression`.
|
70
|
+
|
71
|
+
The only difference between `jsHandle.evaluate` and `jsHandle.evaluateHandle` is that `jsHandle.evaluateHandle` returns
|
72
|
+
[JSHandle](./js_handle).
|
73
|
+
|
74
|
+
If the function passed to the `jsHandle.evaluateHandle` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), then `jsHandle.evaluateHandle` would wait
|
75
|
+
for the promise to resolve and return its value.
|
76
|
+
|
77
|
+
See [Page#evaluate_handle](./page#evaluate_handle) for more details.
|
78
|
+
|
79
|
+
## get_properties
|
80
|
+
|
81
|
+
```
|
82
|
+
def get_properties
|
83
|
+
```
|
84
|
+
alias: `properties`
|
85
|
+
|
86
|
+
The method returns a map with **own property names** as keys and JSHandle instances for the property values.
|
87
|
+
|
88
|
+
```python sync title=example_8292f0e8974d97d20be9bb303d55ccd2d50e42f954e0ada4958ddbef2c6c2977.py
|
89
|
+
handle = page.evaluate_handle("{window, document}")
|
90
|
+
properties = handle.get_properties()
|
91
|
+
window_handle = properties.get("window")
|
92
|
+
document_handle = properties.get("document")
|
93
|
+
handle.dispose()
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
## get_property
|
100
|
+
|
101
|
+
```
|
102
|
+
def get_property(propertyName)
|
103
|
+
```
|
104
|
+
|
105
|
+
Fetches a single property from the referenced object.
|
106
|
+
|
107
|
+
## json_value
|
108
|
+
|
109
|
+
```
|
110
|
+
def json_value
|
111
|
+
```
|
112
|
+
|
113
|
+
Returns a JSON representation of the object. If the object has a `toJSON` function, it **will not be called**.
|
114
|
+
|
115
|
+
> NOTE: The method will return an empty JSON object if the referenced object is not stringifiable. It will throw an
|
116
|
+
error if the object has circular references.
|
@@ -0,0 +1,157 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Keyboard
|
6
|
+
|
7
|
+
Keyboard provides an api for managing a virtual keyboard. The high level api is [Keyboard#type](./keyboard#type), which takes
|
8
|
+
raw characters and generates proper keydown, keypress/input, and keyup events on your page.
|
9
|
+
|
10
|
+
For finer control, you can use [Keyboard#down](./keyboard#down), [Keyboard#up](./keyboard#up), and [Keyboard#insert_text](./keyboard#insert_text)
|
11
|
+
to manually fire events as if they were generated from a real keyboard.
|
12
|
+
|
13
|
+
An example of holding down `Shift` in order to select and delete some text:
|
14
|
+
|
15
|
+
```python sync title=example_575870a45e4fe08d3e06be3420e8a11be03f85791cd8174f27198c016031ae72.py
|
16
|
+
page.keyboard.type("Hello World!")
|
17
|
+
page.keyboard.press("ArrowLeft")
|
18
|
+
page.keyboard.down("Shift")
|
19
|
+
for i in range(6):
|
20
|
+
page.keyboard.press("ArrowLeft")
|
21
|
+
page.keyboard.up("Shift")
|
22
|
+
page.keyboard.press("Backspace")
|
23
|
+
# result text will end up saying "Hello!"
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
An example of pressing uppercase `A`
|
28
|
+
|
29
|
+
```python sync title=example_a4f00f0cd486431b7eca785304f4e9715522da45b66dda7f3a5f6899b889b9fd.py
|
30
|
+
page.keyboard.press("Shift+KeyA")
|
31
|
+
# or
|
32
|
+
page.keyboard.press("Shift+A")
|
33
|
+
|
34
|
+
```
|
35
|
+
|
36
|
+
An example to trigger select-all with the keyboard
|
37
|
+
|
38
|
+
```python sync title=example_2deda0786a20a28cec9e8b438078a5fc567f7c7e5cf369419ab3c4d80a319ff6.py
|
39
|
+
# on windows and linux
|
40
|
+
page.keyboard.press("Control+A")
|
41
|
+
# on mac_os
|
42
|
+
page.keyboard.press("Meta+A")
|
43
|
+
|
44
|
+
```
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
## down
|
49
|
+
|
50
|
+
```
|
51
|
+
def down(key)
|
52
|
+
```
|
53
|
+
|
54
|
+
Dispatches a `keydown` event.
|
55
|
+
|
56
|
+
`key` can specify the intended [keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
|
57
|
+
value or a single character to generate the text for. A superset of the `key` values can be found
|
58
|
+
[here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the keys are:
|
59
|
+
|
60
|
+
`F1` - `F12`, `Digit0`- `Digit9`, `KeyA`- `KeyZ`, `Backquote`, `Minus`, `Equal`, `Backslash`, `Backspace`, `Tab`,
|
61
|
+
`Delete`, `Escape`, `ArrowDown`, `End`, `Enter`, `Home`, `Insert`, `PageDown`, `PageUp`, `ArrowRight`, `ArrowUp`, etc.
|
62
|
+
|
63
|
+
Following modification shortcuts are also supported: `Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`.
|
64
|
+
|
65
|
+
Holding down `Shift` will type the text that corresponds to the `key` in the upper case.
|
66
|
+
|
67
|
+
If `key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different respective
|
68
|
+
texts.
|
69
|
+
|
70
|
+
If `key` is a modifier key, `Shift`, `Meta`, `Control`, or `Alt`, subsequent key presses will be sent with that modifier
|
71
|
+
active. To release the modifier key, use [Keyboard#up](./keyboard#up).
|
72
|
+
|
73
|
+
After the key is pressed once, subsequent calls to [Keyboard#down](./keyboard#down) will have
|
74
|
+
[repeat](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat) set to true. To release the key, use
|
75
|
+
[Keyboard#up](./keyboard#up).
|
76
|
+
|
77
|
+
> NOTE: Modifier keys DO influence `keyboard.down`. Holding down `Shift` will type the text in upper case.
|
78
|
+
|
79
|
+
## insert_text
|
80
|
+
|
81
|
+
```
|
82
|
+
def insert_text(text)
|
83
|
+
```
|
84
|
+
|
85
|
+
Dispatches only `input` event, does not emit the `keydown`, `keyup` or `keypress` events.
|
86
|
+
|
87
|
+
```python sync title=example_a9cc2667e9f3e3b8c619649d7e4a7f5db9463e0b76d67a5e588158093a9e9124.py
|
88
|
+
page.keyboard.insert_text("嗨")
|
89
|
+
|
90
|
+
```
|
91
|
+
|
92
|
+
> NOTE: Modifier keys DO NOT effect `keyboard.insertText`. Holding down `Shift` will not type the text in upper case.
|
93
|
+
|
94
|
+
## press
|
95
|
+
|
96
|
+
```
|
97
|
+
def press(key, delay: nil)
|
98
|
+
```
|
99
|
+
|
100
|
+
`key` can specify the intended [keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
|
101
|
+
value or a single character to generate the text for. A superset of the `key` values can be found
|
102
|
+
[here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the keys are:
|
103
|
+
|
104
|
+
`F1` - `F12`, `Digit0`- `Digit9`, `KeyA`- `KeyZ`, `Backquote`, `Minus`, `Equal`, `Backslash`, `Backspace`, `Tab`,
|
105
|
+
`Delete`, `Escape`, `ArrowDown`, `End`, `Enter`, `Home`, `Insert`, `PageDown`, `PageUp`, `ArrowRight`, `ArrowUp`, etc.
|
106
|
+
|
107
|
+
Following modification shortcuts are also supported: `Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`.
|
108
|
+
|
109
|
+
Holding down `Shift` will type the text that corresponds to the `key` in the upper case.
|
110
|
+
|
111
|
+
If `key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different respective
|
112
|
+
texts.
|
113
|
+
|
114
|
+
Shortcuts such as `key: "Control+o"` or `key: "Control+Shift+T"` are supported as well. When specified with the
|
115
|
+
modifier, modifier is pressed and being held while the subsequent key is being pressed.
|
116
|
+
|
117
|
+
```python sync title=example_88943eb85c1ac7c261601e6edbdead07a31c2784326c496e10667ede1a853bab.py
|
118
|
+
page = browser.new_page()
|
119
|
+
page.goto("https://keycode.info")
|
120
|
+
page.keyboard.press("a")
|
121
|
+
page.screenshot(path="a.png")
|
122
|
+
page.keyboard.press("ArrowLeft")
|
123
|
+
page.screenshot(path="arrow_left.png")
|
124
|
+
page.keyboard.press("Shift+O")
|
125
|
+
page.screenshot(path="o.png")
|
126
|
+
browser.close()
|
127
|
+
|
128
|
+
```
|
129
|
+
|
130
|
+
Shortcut for [Keyboard#down](./keyboard#down) and [Keyboard#up](./keyboard#up).
|
131
|
+
|
132
|
+
## type
|
133
|
+
|
134
|
+
```
|
135
|
+
def type(text, delay: nil)
|
136
|
+
```
|
137
|
+
|
138
|
+
Sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text.
|
139
|
+
|
140
|
+
To press a special key, like `Control` or `ArrowDown`, use [Keyboard#press](./keyboard#press).
|
141
|
+
|
142
|
+
```python sync title=example_d9ced919f139961fd2b795c71375ca96f788a19c1f8e1479c5ec905fb5c02d43.py
|
143
|
+
page.keyboard.type("Hello") # types instantly
|
144
|
+
page.keyboard.type("World", delay=100) # types slower, like a user
|
145
|
+
|
146
|
+
```
|
147
|
+
|
148
|
+
> NOTE: Modifier keys DO NOT effect `keyboard.type`. Holding down `Shift` will not type the text in upper case.
|
149
|
+
> NOTE: For characters that are not on a US keyboard, only an `input` event will be sent.
|
150
|
+
|
151
|
+
## up
|
152
|
+
|
153
|
+
```
|
154
|
+
def up(key)
|
155
|
+
```
|
156
|
+
|
157
|
+
Dispatches a `keyup` event.
|
@@ -0,0 +1,69 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Mouse
|
6
|
+
|
7
|
+
The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport.
|
8
|
+
|
9
|
+
Every `page` object has its own Mouse, accessible with [Page#mouse](./page#mouse).
|
10
|
+
|
11
|
+
```python sync title=example_ba01da1f358cafb4c22b792488ff2f3de4dbd82d4ee1cc4050e3f0c24a2bd7dd.py
|
12
|
+
# using ‘page.mouse’ to trace a 100x100 square.
|
13
|
+
page.mouse.move(0, 0)
|
14
|
+
page.mouse.down()
|
15
|
+
page.mouse.move(0, 100)
|
16
|
+
page.mouse.move(100, 100)
|
17
|
+
page.mouse.move(100, 0)
|
18
|
+
page.mouse.move(0, 0)
|
19
|
+
page.mouse.up()
|
20
|
+
|
21
|
+
```
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
## click
|
26
|
+
|
27
|
+
```
|
28
|
+
def click(
|
29
|
+
x,
|
30
|
+
y,
|
31
|
+
button: nil,
|
32
|
+
clickCount: nil,
|
33
|
+
delay: nil)
|
34
|
+
```
|
35
|
+
|
36
|
+
Shortcut for [Mouse#move](./mouse#move), [Mouse#down](./mouse#down), [Mouse#up](./mouse#up).
|
37
|
+
|
38
|
+
## dblclick
|
39
|
+
|
40
|
+
```
|
41
|
+
def dblclick(x, y, button: nil, delay: nil)
|
42
|
+
```
|
43
|
+
|
44
|
+
Shortcut for [Mouse#move](./mouse#move), [Mouse#down](./mouse#down), [Mouse#up](./mouse#up), [Mouse#down](./mouse#down) and
|
45
|
+
[Mouse#up](./mouse#up).
|
46
|
+
|
47
|
+
## down
|
48
|
+
|
49
|
+
```
|
50
|
+
def down(button: nil, clickCount: nil)
|
51
|
+
```
|
52
|
+
|
53
|
+
Dispatches a `mousedown` event.
|
54
|
+
|
55
|
+
## move
|
56
|
+
|
57
|
+
```
|
58
|
+
def move(x, y, steps: nil)
|
59
|
+
```
|
60
|
+
|
61
|
+
Dispatches a `mousemove` event.
|
62
|
+
|
63
|
+
## up
|
64
|
+
|
65
|
+
```
|
66
|
+
def up(button: nil, clickCount: nil)
|
67
|
+
```
|
68
|
+
|
69
|
+
Dispatches a `mouseup` event.
|
@@ -0,0 +1,1469 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Page
|
6
|
+
|
7
|
+
- extends: [EventEmitter]
|
8
|
+
|
9
|
+
Page provides methods to interact with a single tab in a [Browser](./browser), or an
|
10
|
+
[extension background page](https://developer.chrome.com/extensions/background_pages) in Chromium. One [Browser](./browser)
|
11
|
+
instance might have multiple [Page](./page) instances.
|
12
|
+
|
13
|
+
This example creates a page, navigates it to a URL, and then saves a screenshot:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
playwright.webkit.launch do |browser|
|
17
|
+
page = browser.new_page
|
18
|
+
page.goto('https://example.com/')
|
19
|
+
page.screenshot(path: 'screenshot.png')
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
The Page class emits various events (described below) which can be handled using any of Node's native
|
24
|
+
[`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) methods, such as `on`, `once` or
|
25
|
+
`removeListener`.
|
26
|
+
|
27
|
+
This example logs a message for a single page `load` event:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
page.once("load", -> (page) { puts "page loaded!" })
|
31
|
+
```
|
32
|
+
|
33
|
+
To unsubscribe from events use the `removeListener` method:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
listener = -> (req) { puts "a request was made: #{req.url}" }
|
37
|
+
page.on('request', listener)
|
38
|
+
page.goto('https://example.com/') # => prints 'a request was made: https://example.com/'
|
39
|
+
page.off('request', listener)
|
40
|
+
page.goto('https://example.com/') # => no print
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
## add_init_script
|
46
|
+
|
47
|
+
```
|
48
|
+
def add_init_script(path: nil, script: nil)
|
49
|
+
```
|
50
|
+
|
51
|
+
Adds a script which would be evaluated in one of the following scenarios:
|
52
|
+
- Whenever the page is navigated.
|
53
|
+
- Whenever the child frame is attached or navigated. In this case, the script is evaluated in the context of the newly
|
54
|
+
attached frame.
|
55
|
+
|
56
|
+
The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend
|
57
|
+
the JavaScript environment, e.g. to seed `Math.random`.
|
58
|
+
|
59
|
+
An example of overriding `Math.random` before the page loads:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# in your playwright script, assuming the preload.js file is in same directory
|
63
|
+
page.add_init_script(path: "./preload.js")
|
64
|
+
```
|
65
|
+
|
66
|
+
> NOTE: The order of evaluation of multiple scripts installed via [BrowserContext#add_init_script](./browser_context#add_init_script) and
|
67
|
+
[Page#add_init_script](./page#add_init_script) is not defined.
|
68
|
+
|
69
|
+
## add_script_tag
|
70
|
+
|
71
|
+
```
|
72
|
+
def add_script_tag(content: nil, path: nil, type: nil, url: nil)
|
73
|
+
```
|
74
|
+
|
75
|
+
Adds a `<script>` tag into the page with the desired url or content. Returns the added tag when the script's onload
|
76
|
+
fires or when the script content was injected into frame.
|
77
|
+
|
78
|
+
Shortcut for main frame's [Frame#add_script_tag](./frame#add_script_tag).
|
79
|
+
|
80
|
+
## add_style_tag
|
81
|
+
|
82
|
+
```
|
83
|
+
def add_style_tag(content: nil, path: nil, url: nil)
|
84
|
+
```
|
85
|
+
|
86
|
+
Adds a `<link rel="stylesheet">` tag into the page with the desired url or a `<style type="text/css">` tag with the
|
87
|
+
content. Returns the added tag when the stylesheet's onload fires or when the CSS content was injected into frame.
|
88
|
+
|
89
|
+
Shortcut for main frame's [Frame#add_style_tag](./frame#add_style_tag).
|
90
|
+
|
91
|
+
## bring_to_front
|
92
|
+
|
93
|
+
```
|
94
|
+
def bring_to_front
|
95
|
+
```
|
96
|
+
|
97
|
+
Brings page to front (activates tab).
|
98
|
+
|
99
|
+
## check
|
100
|
+
|
101
|
+
```
|
102
|
+
def check(
|
103
|
+
selector,
|
104
|
+
force: nil,
|
105
|
+
noWaitAfter: nil,
|
106
|
+
position: nil,
|
107
|
+
timeout: nil,
|
108
|
+
trial: nil)
|
109
|
+
```
|
110
|
+
|
111
|
+
This method checks an element matching `selector` by performing the following steps:
|
112
|
+
1. Find an element matching `selector`. If there is none, wait until a matching element is attached to the DOM.
|
113
|
+
1. Ensure that matched element is a checkbox or a radio input. If not, this method throws. If the element is already
|
114
|
+
checked, this method returns immediately.
|
115
|
+
1. Wait for [actionability](https://playwright.dev/python/docs/actionability) checks on the matched element, unless `force` option is set. If the
|
116
|
+
element is detached during the checks, the whole action is retried.
|
117
|
+
1. Scroll the element into view if needed.
|
118
|
+
1. Use [Page#mouse](./page#mouse) to click in the center of the element.
|
119
|
+
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
|
120
|
+
1. Ensure that the element is now checked. If not, this method throws.
|
121
|
+
|
122
|
+
When all steps combined have not finished during the specified `timeout`, this method throws a `TimeoutError`. Passing
|
123
|
+
zero timeout disables this.
|
124
|
+
|
125
|
+
Shortcut for main frame's [Frame#check](./frame#check).
|
126
|
+
|
127
|
+
## click
|
128
|
+
|
129
|
+
```
|
130
|
+
def click(
|
131
|
+
selector,
|
132
|
+
button: nil,
|
133
|
+
clickCount: nil,
|
134
|
+
delay: nil,
|
135
|
+
force: nil,
|
136
|
+
modifiers: nil,
|
137
|
+
noWaitAfter: nil,
|
138
|
+
position: nil,
|
139
|
+
timeout: nil,
|
140
|
+
trial: nil)
|
141
|
+
```
|
142
|
+
|
143
|
+
This method clicks an element matching `selector` by performing the following steps:
|
144
|
+
1. Find an element matching `selector`. If there is none, wait until a matching element is attached to the DOM.
|
145
|
+
1. Wait for [actionability](https://playwright.dev/python/docs/actionability) checks on the matched element, unless `force` option is set. If the
|
146
|
+
element is detached during the checks, the whole action is retried.
|
147
|
+
1. Scroll the element into view if needed.
|
148
|
+
1. Use [Page#mouse](./page#mouse) to click in the center of the element, or the specified `position`.
|
149
|
+
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
|
150
|
+
|
151
|
+
When all steps combined have not finished during the specified `timeout`, this method throws a `TimeoutError`. Passing
|
152
|
+
zero timeout disables this.
|
153
|
+
|
154
|
+
Shortcut for main frame's [Frame#click](./frame#click).
|
155
|
+
|
156
|
+
## close
|
157
|
+
|
158
|
+
```
|
159
|
+
def close(runBeforeUnload: nil)
|
160
|
+
```
|
161
|
+
|
162
|
+
If `runBeforeUnload` is `false`, does not run any unload handlers and waits for the page to be closed. If
|
163
|
+
`runBeforeUnload` is `true` the method will run unload handlers, but will **not** wait for the page to close.
|
164
|
+
|
165
|
+
By default, `page.close()` **does not** run `beforeunload` handlers.
|
166
|
+
|
167
|
+
> NOTE: if `runBeforeUnload` is passed as true, a `beforeunload` dialog might be summoned and should be handled manually
|
168
|
+
via [`event: Page.dialog`] event.
|
169
|
+
|
170
|
+
## content
|
171
|
+
|
172
|
+
```
|
173
|
+
def content
|
174
|
+
```
|
175
|
+
|
176
|
+
Gets the full HTML contents of the page, including the doctype.
|
177
|
+
|
178
|
+
## context
|
179
|
+
|
180
|
+
```
|
181
|
+
def context
|
182
|
+
```
|
183
|
+
|
184
|
+
Get the browser context that the page belongs to.
|
185
|
+
|
186
|
+
## dblclick
|
187
|
+
|
188
|
+
```
|
189
|
+
def dblclick(
|
190
|
+
selector,
|
191
|
+
button: nil,
|
192
|
+
delay: nil,
|
193
|
+
force: nil,
|
194
|
+
modifiers: nil,
|
195
|
+
noWaitAfter: nil,
|
196
|
+
position: nil,
|
197
|
+
timeout: nil,
|
198
|
+
trial: nil)
|
199
|
+
```
|
200
|
+
|
201
|
+
This method double clicks an element matching `selector` by performing the following steps:
|
202
|
+
1. Find an element matching `selector`. If there is none, wait until a matching element is attached to the DOM.
|
203
|
+
1. Wait for [actionability](https://playwright.dev/python/docs/actionability) checks on the matched element, unless `force` option is set. If the
|
204
|
+
element is detached during the checks, the whole action is retried.
|
205
|
+
1. Scroll the element into view if needed.
|
206
|
+
1. Use [Page#mouse](./page#mouse) to double click in the center of the element, or the specified `position`.
|
207
|
+
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set. Note that if the
|
208
|
+
first click of the `dblclick()` triggers a navigation event, this method will throw.
|
209
|
+
|
210
|
+
When all steps combined have not finished during the specified `timeout`, this method throws a `TimeoutError`. Passing
|
211
|
+
zero timeout disables this.
|
212
|
+
|
213
|
+
> NOTE: `page.dblclick()` dispatches two `click` events and a single `dblclick` event.
|
214
|
+
|
215
|
+
Shortcut for main frame's [Frame#dblclick](./frame#dblclick).
|
216
|
+
|
217
|
+
## dispatch_event
|
218
|
+
|
219
|
+
```
|
220
|
+
def dispatch_event(selector, type, eventInit: nil, timeout: nil)
|
221
|
+
```
|
222
|
+
|
223
|
+
The snippet below dispatches the `click` event on the element. Regardless of the visibility state of the element,
|
224
|
+
`click` is dispatched. This is equivalent to calling
|
225
|
+
[element.click()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click).
|
226
|
+
|
227
|
+
```python sync title=example_9220b94fd2fa381ab91448dcb551e2eb9806ad331c83454a710f4d8a280990e8.py
|
228
|
+
page.dispatch_event("button#submit", "click")
|
229
|
+
|
230
|
+
```
|
231
|
+
|
232
|
+
Under the hood, it creates an instance of an event based on the given `type`, initializes it with `eventInit` properties
|
233
|
+
and dispatches it on the element. Events are `composed`, `cancelable` and bubble by default.
|
234
|
+
|
235
|
+
Since `eventInit` is event-specific, please refer to the events documentation for the lists of initial properties:
|
236
|
+
- [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent/DragEvent)
|
237
|
+
- [FocusEvent](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/FocusEvent)
|
238
|
+
- [KeyboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent)
|
239
|
+
- [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent)
|
240
|
+
- [PointerEvent](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent)
|
241
|
+
- [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent)
|
242
|
+
- [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event)
|
243
|
+
|
244
|
+
You can also specify [JSHandle](./js_handle) as the property value if you want live objects to be passed into the event:
|
245
|
+
|
246
|
+
```python sync title=example_9b4482b7243b7ce304d6ce8454395e23db30f3d1d83229242ab7bd2abd5b72e0.py
|
247
|
+
# note you can only create data_transfer in chromium and firefox
|
248
|
+
data_transfer = page.evaluate_handle("new DataTransfer()")
|
249
|
+
page.dispatch_event("#source", "dragstart", { "dataTransfer": data_transfer })
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
## emulate_media
|
256
|
+
|
257
|
+
```
|
258
|
+
def emulate_media(colorScheme: nil, media: nil)
|
259
|
+
```
|
260
|
+
|
261
|
+
This method changes the `CSS media type` through the `media` argument, and/or the `'prefers-colors-scheme'` media
|
262
|
+
feature, using the `colorScheme` argument.
|
263
|
+
|
264
|
+
```python sync title=example_df304caf6c61f6f44b3e2b0006a7e05552362a47b17c9ba227df76e918d88a5c.py
|
265
|
+
page.evaluate("matchMedia('screen').matches")
|
266
|
+
# → True
|
267
|
+
page.evaluate("matchMedia('print').matches")
|
268
|
+
# → False
|
269
|
+
|
270
|
+
page.emulate_media(media="print")
|
271
|
+
page.evaluate("matchMedia('screen').matches")
|
272
|
+
# → False
|
273
|
+
page.evaluate("matchMedia('print').matches")
|
274
|
+
# → True
|
275
|
+
|
276
|
+
page.emulate_media()
|
277
|
+
page.evaluate("matchMedia('screen').matches")
|
278
|
+
# → True
|
279
|
+
page.evaluate("matchMedia('print').matches")
|
280
|
+
# → False
|
281
|
+
|
282
|
+
```
|
283
|
+
|
284
|
+
```python sync title=example_f0479a2ee8d8f51dab94f48b7e121cade07e5026d4f602521cc6ccc47feb5a98.py
|
285
|
+
page.emulate_media(color_scheme="dark")
|
286
|
+
page.evaluate("matchMedia('(prefers-color-scheme: dark)').matches")
|
287
|
+
# → True
|
288
|
+
page.evaluate("matchMedia('(prefers-color-scheme: light)').matches")
|
289
|
+
# → False
|
290
|
+
page.evaluate("matchMedia('(prefers-color-scheme: no-preference)').matches")
|
291
|
+
|
292
|
+
```
|
293
|
+
|
294
|
+
|
295
|
+
|
296
|
+
## eval_on_selector
|
297
|
+
|
298
|
+
```
|
299
|
+
def eval_on_selector(selector, expression, arg: nil)
|
300
|
+
```
|
301
|
+
|
302
|
+
The method finds an element matching the specified selector within the page and passes it as a first argument to
|
303
|
+
`expression`. If no elements match the selector, the method throws an error. Returns the value of `expression`.
|
304
|
+
|
305
|
+
If `expression` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), then [Page#eval_on_selector](./page#eval_on_selector) would wait for the promise to resolve and
|
306
|
+
return its value.
|
307
|
+
|
308
|
+
Examples:
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
search_value = page.eval_on_selector("#search", "el => el.value")
|
312
|
+
preload_href = page.eval_on_selector("link[rel=preload]", "el => el.href")
|
313
|
+
html = page.eval_on_selector(".main-container", "(e, suffix) => e.outer_html + suffix", arg: "hello")
|
314
|
+
```
|
315
|
+
|
316
|
+
Shortcut for main frame's [Frame#eval_on_selector](./frame#eval_on_selector).
|
317
|
+
|
318
|
+
## eval_on_selector_all
|
319
|
+
|
320
|
+
```
|
321
|
+
def eval_on_selector_all(selector, expression, arg: nil)
|
322
|
+
```
|
323
|
+
|
324
|
+
The method finds all elements matching the specified selector within the page and passes an array of matched elements as
|
325
|
+
a first argument to `expression`. Returns the result of `expression` invocation.
|
326
|
+
|
327
|
+
If `expression` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), then [Page#eval_on_selector_all](./page#eval_on_selector_all) would wait for the promise to resolve and
|
328
|
+
return its value.
|
329
|
+
|
330
|
+
Examples:
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
div_counts = page.eval_on_selector_all("div", "(divs, min) => divs.length >= min", arg: 10)
|
334
|
+
```
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
## evaluate
|
339
|
+
|
340
|
+
```
|
341
|
+
def evaluate(expression, arg: nil)
|
342
|
+
```
|
343
|
+
|
344
|
+
Returns the value of the `expression` invocation.
|
345
|
+
|
346
|
+
If the function passed to the [Page#evaluate](./page#evaluate) returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), then [Page#evaluate](./page#evaluate) would wait
|
347
|
+
for the promise to resolve and return its value.
|
348
|
+
|
349
|
+
If the function passed to the [Page#evaluate](./page#evaluate) returns a non-[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description) value, then
|
350
|
+
[Page#evaluate](./page#evaluate) resolves to `undefined`. Playwright also supports transferring some additional values that are
|
351
|
+
not serializable by `JSON`: `-0`, `NaN`, `Infinity`, `-Infinity`.
|
352
|
+
|
353
|
+
Passing argument to `expression`:
|
354
|
+
|
355
|
+
```ruby
|
356
|
+
result = page.evaluate("([x, y]) => Promise.resolve(x * y)", arg: [7, 8])
|
357
|
+
puts result # => "56"
|
358
|
+
```
|
359
|
+
|
360
|
+
A string can also be passed in instead of a function:
|
361
|
+
|
362
|
+
```ruby
|
363
|
+
puts page.evaluate("1 + 2") # => 3
|
364
|
+
x = 10
|
365
|
+
puts page.evaluate("1 + #{x}") # => "11"
|
366
|
+
```
|
367
|
+
|
368
|
+
[ElementHandle](./element_handle) instances can be passed as an argument to the [Page#evaluate](./page#evaluate):
|
369
|
+
|
370
|
+
```ruby
|
371
|
+
body_handle = page.query_selector("body")
|
372
|
+
html = page.evaluate("([body, suffix]) => body.innerHTML + suffix", arg: [body_handle, "hello"])
|
373
|
+
body_handle.dispose()
|
374
|
+
```
|
375
|
+
|
376
|
+
Shortcut for main frame's [Frame#evaluate](./frame#evaluate).
|
377
|
+
|
378
|
+
## evaluate_handle
|
379
|
+
|
380
|
+
```
|
381
|
+
def evaluate_handle(expression, arg: nil)
|
382
|
+
```
|
383
|
+
|
384
|
+
Returns the value of the `expression` invocation as a [JSHandle](./js_handle).
|
385
|
+
|
386
|
+
The only difference between [Page#evaluate](./page#evaluate) and [Page#evaluate_handle](./page#evaluate_handle) is that
|
387
|
+
[Page#evaluate_handle](./page#evaluate_handle) returns [JSHandle](./js_handle).
|
388
|
+
|
389
|
+
If the function passed to the [Page#evaluate_handle](./page#evaluate_handle) returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), then [Page#evaluate_handle](./page#evaluate_handle)
|
390
|
+
would wait for the promise to resolve and return its value.
|
391
|
+
|
392
|
+
```python sync title=example_6802829f93cc4da7e67f3886b9773c7b84054afa84251add50704f8ca6837138.py
|
393
|
+
a_window_handle = page.evaluate_handle("Promise.resolve(window)")
|
394
|
+
a_window_handle # handle for the window object.
|
395
|
+
|
396
|
+
```
|
397
|
+
|
398
|
+
A string can also be passed in instead of a function:
|
399
|
+
|
400
|
+
```python sync title=example_9daa37cfd3d747c9360d9544f64786bf49d291a6887b0efccc813215b62ae4c6.py
|
401
|
+
a_handle = page.evaluate_handle("document") # handle for the "document"
|
402
|
+
|
403
|
+
```
|
404
|
+
|
405
|
+
[JSHandle](./js_handle) instances can be passed as an argument to the [Page#evaluate_handle](./page#evaluate_handle):
|
406
|
+
|
407
|
+
```ruby
|
408
|
+
body_handle = page.evaluate_handle("document.body")
|
409
|
+
result_handle = page.evaluate_handle("body => body.innerHTML", arg: body_handle)
|
410
|
+
puts result_handle.json_value()
|
411
|
+
result_handle.dispose()
|
412
|
+
```
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
## expose_binding
|
417
|
+
|
418
|
+
```
|
419
|
+
def expose_binding(name, callback, handle: nil)
|
420
|
+
```
|
421
|
+
|
422
|
+
The method adds a function called `name` on the `window` object of every frame in this page. When called, the function
|
423
|
+
executes `callback` and returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) which resolves to the return value of `callback`. If the `callback` returns
|
424
|
+
a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), it will be awaited.
|
425
|
+
|
426
|
+
The first argument of the `callback` function contains information about the caller: `{ browserContext: BrowserContext,
|
427
|
+
page: Page, frame: Frame }`.
|
428
|
+
|
429
|
+
See [BrowserContext#expose_binding](./browser_context#expose_binding) for the context-wide version.
|
430
|
+
|
431
|
+
> NOTE: Functions installed via [Page#expose_binding](./page#expose_binding) survive navigations.
|
432
|
+
|
433
|
+
An example of exposing page URL to all frames in a page:
|
434
|
+
|
435
|
+
```python sync title=example_551f5963351bfd7141fa8c94f5f22c305ec1c01d617861953374e9290929a551.py
|
436
|
+
from playwright.sync_api import sync_playwright
|
437
|
+
|
438
|
+
def run(playwright):
|
439
|
+
webkit = playwright.webkit
|
440
|
+
browser = webkit.launch(headless=false)
|
441
|
+
context = browser.new_context()
|
442
|
+
page = context.new_page()
|
443
|
+
page.expose_binding("pageURL", lambda source: source["page"].url)
|
444
|
+
page.set_content("""
|
445
|
+
<script>
|
446
|
+
async function onClick() {
|
447
|
+
document.querySelector('div').textContent = await window.pageURL();
|
448
|
+
}
|
449
|
+
</script>
|
450
|
+
<button onclick="onClick()">Click me</button>
|
451
|
+
<div></div>
|
452
|
+
""")
|
453
|
+
page.click("button")
|
454
|
+
|
455
|
+
with sync_playwright() as playwright:
|
456
|
+
run(playwright)
|
457
|
+
|
458
|
+
```
|
459
|
+
|
460
|
+
An example of passing an element handle:
|
461
|
+
|
462
|
+
```python sync title=example_6534a792e99e05b5644cea6e5b77ca5d864675a3012f447f0f8318c4fa6a6a54.py
|
463
|
+
def print(source, element):
|
464
|
+
print(element.text_content())
|
465
|
+
|
466
|
+
page.expose_binding("clicked", print, handle=true)
|
467
|
+
page.set_content("""
|
468
|
+
<script>
|
469
|
+
document.addEventListener('click', event => window.clicked(event.target));
|
470
|
+
</script>
|
471
|
+
<div>Click me</div>
|
472
|
+
<div>Or click me</div>
|
473
|
+
""")
|
474
|
+
|
475
|
+
```
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
## expose_function
|
480
|
+
|
481
|
+
```
|
482
|
+
def expose_function(name, callback)
|
483
|
+
```
|
484
|
+
|
485
|
+
The method adds a function called `name` on the `window` object of every frame in the page. When called, the function
|
486
|
+
executes `callback` and returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) which resolves to the return value of `callback`.
|
487
|
+
|
488
|
+
If the `callback` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), it will be awaited.
|
489
|
+
|
490
|
+
See [BrowserContext#expose_function](./browser_context#expose_function) for context-wide exposed function.
|
491
|
+
|
492
|
+
> NOTE: Functions installed via [Page#expose_function](./page#expose_function) survive navigations.
|
493
|
+
|
494
|
+
An example of adding an `sha1` function to the page:
|
495
|
+
|
496
|
+
```python sync title=example_496ab45e0c5f4c47869f66c2b738fbd9eef0ef4065fa923caf9c929e50e14c21.py
|
497
|
+
import hashlib
|
498
|
+
from playwright.sync_api import sync_playwright
|
499
|
+
|
500
|
+
def sha1(text):
|
501
|
+
m = hashlib.sha1()
|
502
|
+
m.update(bytes(text, "utf8"))
|
503
|
+
return m.hexdigest()
|
504
|
+
|
505
|
+
|
506
|
+
def run(playwright):
|
507
|
+
webkit = playwright.webkit
|
508
|
+
browser = webkit.launch(headless=False)
|
509
|
+
page = browser.new_page()
|
510
|
+
page.expose_function("sha1", sha1)
|
511
|
+
page.set_content("""
|
512
|
+
<script>
|
513
|
+
async function onClick() {
|
514
|
+
document.querySelector('div').textContent = await window.sha1('PLAYWRIGHT');
|
515
|
+
}
|
516
|
+
</script>
|
517
|
+
<button onclick="onClick()">Click me</button>
|
518
|
+
<div></div>
|
519
|
+
""")
|
520
|
+
page.click("button")
|
521
|
+
|
522
|
+
with sync_playwright() as playwright:
|
523
|
+
run(playwright)
|
524
|
+
|
525
|
+
```
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
## fill
|
530
|
+
|
531
|
+
```
|
532
|
+
def fill(selector, value, noWaitAfter: nil, timeout: nil)
|
533
|
+
```
|
534
|
+
|
535
|
+
This method waits for an element matching `selector`, waits for [actionability](https://playwright.dev/python/docs/actionability) checks, focuses the
|
536
|
+
element, fills it and triggers an `input` event after filling. Note that you can pass an empty string to clear the input
|
537
|
+
field.
|
538
|
+
|
539
|
+
If the target element is not an `<input>`, `<textarea>` or `[contenteditable]` element, this method throws an error.
|
540
|
+
However, if the element is inside the `<label>` element that has an associated
|
541
|
+
[control](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement/control), the control will be filled
|
542
|
+
instead.
|
543
|
+
|
544
|
+
To send fine-grained keyboard events, use [Page#type](./page#type).
|
545
|
+
|
546
|
+
Shortcut for main frame's [Frame#fill](./frame#fill).
|
547
|
+
|
548
|
+
## focus
|
549
|
+
|
550
|
+
```
|
551
|
+
def focus(selector, timeout: nil)
|
552
|
+
```
|
553
|
+
|
554
|
+
This method fetches an element with `selector` and focuses it. If there's no element matching `selector`, the method
|
555
|
+
waits until a matching element appears in the DOM.
|
556
|
+
|
557
|
+
Shortcut for main frame's [Frame#focus](./frame#focus).
|
558
|
+
|
559
|
+
## frame
|
560
|
+
|
561
|
+
```
|
562
|
+
def frame(name: nil, url: nil)
|
563
|
+
```
|
564
|
+
|
565
|
+
Returns frame matching the specified criteria. Either `name` or `url` must be specified.
|
566
|
+
|
567
|
+
```py title=example_034f224ec0f7b4d98fdf875cefbc7e6c8726a6d615cbba9b1cb8c49180fd7d69.py
|
568
|
+
frame = page.frame(name="frame-name")
|
569
|
+
|
570
|
+
```
|
571
|
+
|
572
|
+
```py title=example_a8a4717d8505a35662faafa9e6c2cfbbc0a44755c8e4d43252f882b7e4f1f04a.py
|
573
|
+
frame = page.frame(url=r".*domain.*")
|
574
|
+
|
575
|
+
```
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
## frames
|
580
|
+
|
581
|
+
```
|
582
|
+
def frames
|
583
|
+
```
|
584
|
+
|
585
|
+
An array of all frames attached to the page.
|
586
|
+
|
587
|
+
## get_attribute
|
588
|
+
|
589
|
+
```
|
590
|
+
def get_attribute(selector, name, timeout: nil)
|
591
|
+
```
|
592
|
+
|
593
|
+
Returns element attribute value.
|
594
|
+
|
595
|
+
## go_back
|
596
|
+
|
597
|
+
```
|
598
|
+
def go_back(timeout: nil, waitUntil: nil)
|
599
|
+
```
|
600
|
+
|
601
|
+
Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the
|
602
|
+
last redirect. If can not go back, returns `null`.
|
603
|
+
|
604
|
+
Navigate to the previous page in history.
|
605
|
+
|
606
|
+
## go_forward
|
607
|
+
|
608
|
+
```
|
609
|
+
def go_forward(timeout: nil, waitUntil: nil)
|
610
|
+
```
|
611
|
+
|
612
|
+
Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the
|
613
|
+
last redirect. If can not go forward, returns `null`.
|
614
|
+
|
615
|
+
Navigate to the next page in history.
|
616
|
+
|
617
|
+
## goto
|
618
|
+
|
619
|
+
```
|
620
|
+
def goto(url, referer: nil, timeout: nil, waitUntil: nil)
|
621
|
+
```
|
622
|
+
|
623
|
+
Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the
|
624
|
+
last redirect.
|
625
|
+
|
626
|
+
`page.goto` will throw an error if:
|
627
|
+
- there's an SSL error (e.g. in case of self-signed certificates).
|
628
|
+
- target URL is invalid.
|
629
|
+
- the `timeout` is exceeded during navigation.
|
630
|
+
- the remote server does not respond or is unreachable.
|
631
|
+
- the main resource failed to load.
|
632
|
+
|
633
|
+
`page.goto` will not throw an error when any valid HTTP status code is returned by the remote server, including 404 "Not
|
634
|
+
Found" and 500 "Internal Server Error". The status code for such responses can be retrieved by calling
|
635
|
+
[Response#status](./response#status).
|
636
|
+
|
637
|
+
> NOTE: `page.goto` either throws an error or returns a main resource response. The only exceptions are navigation to
|
638
|
+
`about:blank` or navigation to the same URL with a different hash, which would succeed and return `null`.
|
639
|
+
> NOTE: Headless mode doesn't support navigation to a PDF document. See the
|
640
|
+
[upstream issue](https://bugs.chromium.org/p/chromium/issues/detail?id=761295).
|
641
|
+
|
642
|
+
Shortcut for main frame's [Frame#goto](./frame#goto)
|
643
|
+
|
644
|
+
## hover
|
645
|
+
|
646
|
+
```
|
647
|
+
def hover(
|
648
|
+
selector,
|
649
|
+
force: nil,
|
650
|
+
modifiers: nil,
|
651
|
+
position: nil,
|
652
|
+
timeout: nil,
|
653
|
+
trial: nil)
|
654
|
+
```
|
655
|
+
|
656
|
+
This method hovers over an element matching `selector` by performing the following steps:
|
657
|
+
1. Find an element matching `selector`. If there is none, wait until a matching element is attached to the DOM.
|
658
|
+
1. Wait for [actionability](https://playwright.dev/python/docs/actionability) checks on the matched element, unless `force` option is set. If the
|
659
|
+
element is detached during the checks, the whole action is retried.
|
660
|
+
1. Scroll the element into view if needed.
|
661
|
+
1. Use [Page#mouse](./page#mouse) to hover over the center of the element, or the specified `position`.
|
662
|
+
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
|
663
|
+
|
664
|
+
When all steps combined have not finished during the specified `timeout`, this method throws a `TimeoutError`. Passing
|
665
|
+
zero timeout disables this.
|
666
|
+
|
667
|
+
Shortcut for main frame's [Frame#hover](./frame#hover).
|
668
|
+
|
669
|
+
## inner_html
|
670
|
+
|
671
|
+
```
|
672
|
+
def inner_html(selector, timeout: nil)
|
673
|
+
```
|
674
|
+
|
675
|
+
Returns `element.innerHTML`.
|
676
|
+
|
677
|
+
## inner_text
|
678
|
+
|
679
|
+
```
|
680
|
+
def inner_text(selector, timeout: nil)
|
681
|
+
```
|
682
|
+
|
683
|
+
Returns `element.innerText`.
|
684
|
+
|
685
|
+
## checked?
|
686
|
+
|
687
|
+
```
|
688
|
+
def checked?(selector, timeout: nil)
|
689
|
+
```
|
690
|
+
|
691
|
+
Returns whether the element is checked. Throws if the element is not a checkbox or radio input.
|
692
|
+
|
693
|
+
## closed?
|
694
|
+
|
695
|
+
```
|
696
|
+
def closed?
|
697
|
+
```
|
698
|
+
|
699
|
+
Indicates that the page has been closed.
|
700
|
+
|
701
|
+
## disabled?
|
702
|
+
|
703
|
+
```
|
704
|
+
def disabled?(selector, timeout: nil)
|
705
|
+
```
|
706
|
+
|
707
|
+
Returns whether the element is disabled, the opposite of [enabled](https://playwright.dev/python/docs/actionability).
|
708
|
+
|
709
|
+
## editable?
|
710
|
+
|
711
|
+
```
|
712
|
+
def editable?(selector, timeout: nil)
|
713
|
+
```
|
714
|
+
|
715
|
+
Returns whether the element is [editable](https://playwright.dev/python/docs/actionability).
|
716
|
+
|
717
|
+
## enabled?
|
718
|
+
|
719
|
+
```
|
720
|
+
def enabled?(selector, timeout: nil)
|
721
|
+
```
|
722
|
+
|
723
|
+
Returns whether the element is [enabled](https://playwright.dev/python/docs/actionability).
|
724
|
+
|
725
|
+
## hidden?
|
726
|
+
|
727
|
+
```
|
728
|
+
def hidden?(selector, timeout: nil)
|
729
|
+
```
|
730
|
+
|
731
|
+
Returns whether the element is hidden, the opposite of [visible](https://playwright.dev/python/docs/actionability). `selector` that does not
|
732
|
+
match any elements is considered hidden.
|
733
|
+
|
734
|
+
## visible?
|
735
|
+
|
736
|
+
```
|
737
|
+
def visible?(selector, timeout: nil)
|
738
|
+
```
|
739
|
+
|
740
|
+
Returns whether the element is [visible](https://playwright.dev/python/docs/actionability). `selector` that does not match any elements is
|
741
|
+
considered not visible.
|
742
|
+
|
743
|
+
## main_frame
|
744
|
+
|
745
|
+
```
|
746
|
+
def main_frame
|
747
|
+
```
|
748
|
+
|
749
|
+
The page's main frame. Page is guaranteed to have a main frame which persists during navigations.
|
750
|
+
|
751
|
+
## opener
|
752
|
+
|
753
|
+
```
|
754
|
+
def opener
|
755
|
+
```
|
756
|
+
|
757
|
+
Returns the opener for popup pages and `null` for others. If the opener has been closed already the returns `null`.
|
758
|
+
|
759
|
+
## pdf
|
760
|
+
|
761
|
+
```
|
762
|
+
def pdf(
|
763
|
+
displayHeaderFooter: nil,
|
764
|
+
footerTemplate: nil,
|
765
|
+
format: nil,
|
766
|
+
headerTemplate: nil,
|
767
|
+
height: nil,
|
768
|
+
landscape: nil,
|
769
|
+
margin: nil,
|
770
|
+
pageRanges: nil,
|
771
|
+
path: nil,
|
772
|
+
preferCSSPageSize: nil,
|
773
|
+
printBackground: nil,
|
774
|
+
scale: nil,
|
775
|
+
width: nil)
|
776
|
+
```
|
777
|
+
|
778
|
+
Returns the PDF buffer.
|
779
|
+
|
780
|
+
> NOTE: Generating a pdf is currently only supported in Chromium headless.
|
781
|
+
|
782
|
+
`page.pdf()` generates a pdf of the page with `print` css media. To generate a pdf with `screen` media, call
|
783
|
+
[Page#emulate_media](./page#emulate_media) before calling `page.pdf()`:
|
784
|
+
|
785
|
+
> NOTE: By default, `page.pdf()` generates a pdf with modified colors for printing. Use the
|
786
|
+
[`-webkit-print-color-adjust`](https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-print-color-adjust) property to
|
787
|
+
force rendering of exact colors.
|
788
|
+
|
789
|
+
```python sync title=example_e079fbec8ee0607ee45cdca94df61dea36f7fd3840986d5f4ac24918569a5f5e.py
|
790
|
+
# generates a pdf with "screen" media type.
|
791
|
+
page.emulate_media(media="screen")
|
792
|
+
page.pdf(path="page.pdf")
|
793
|
+
|
794
|
+
```
|
795
|
+
|
796
|
+
The `width`, `height`, and `margin` options accept values labeled with units. Unlabeled values are treated as pixels.
|
797
|
+
|
798
|
+
A few examples:
|
799
|
+
- `page.pdf({width: 100})` - prints with width set to 100 pixels
|
800
|
+
- `page.pdf({width: '100px'})` - prints with width set to 100 pixels
|
801
|
+
- `page.pdf({width: '10cm'})` - prints with width set to 10 centimeters.
|
802
|
+
|
803
|
+
All possible units are:
|
804
|
+
- `px` - pixel
|
805
|
+
- `in` - inch
|
806
|
+
- `cm` - centimeter
|
807
|
+
- `mm` - millimeter
|
808
|
+
|
809
|
+
The `format` options are:
|
810
|
+
- `Letter`: 8.5in x 11in
|
811
|
+
- `Legal`: 8.5in x 14in
|
812
|
+
- `Tabloid`: 11in x 17in
|
813
|
+
- `Ledger`: 17in x 11in
|
814
|
+
- `A0`: 33.1in x 46.8in
|
815
|
+
- `A1`: 23.4in x 33.1in
|
816
|
+
- `A2`: 16.54in x 23.4in
|
817
|
+
- `A3`: 11.7in x 16.54in
|
818
|
+
- `A4`: 8.27in x 11.7in
|
819
|
+
- `A5`: 5.83in x 8.27in
|
820
|
+
- `A6`: 4.13in x 5.83in
|
821
|
+
|
822
|
+
> NOTE: `headerTemplate` and `footerTemplate` markup have the following limitations: > 1. Script tags inside templates
|
823
|
+
are not evaluated. > 2. Page styles are not visible inside templates.
|
824
|
+
|
825
|
+
## press
|
826
|
+
|
827
|
+
```
|
828
|
+
def press(
|
829
|
+
selector,
|
830
|
+
key,
|
831
|
+
delay: nil,
|
832
|
+
noWaitAfter: nil,
|
833
|
+
timeout: nil)
|
834
|
+
```
|
835
|
+
|
836
|
+
Focuses the element, and then uses [Keyboard#down](./keyboard#down) and [Keyboard#up](./keyboard#up).
|
837
|
+
|
838
|
+
`key` can specify the intended [keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
|
839
|
+
value or a single character to generate the text for. A superset of the `key` values can be found
|
840
|
+
[here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the keys are:
|
841
|
+
|
842
|
+
`F1` - `F12`, `Digit0`- `Digit9`, `KeyA`- `KeyZ`, `Backquote`, `Minus`, `Equal`, `Backslash`, `Backspace`, `Tab`,
|
843
|
+
`Delete`, `Escape`, `ArrowDown`, `End`, `Enter`, `Home`, `Insert`, `PageDown`, `PageUp`, `ArrowRight`, `ArrowUp`, etc.
|
844
|
+
|
845
|
+
Following modification shortcuts are also supported: `Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`.
|
846
|
+
|
847
|
+
Holding down `Shift` will type the text that corresponds to the `key` in the upper case.
|
848
|
+
|
849
|
+
If `key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different respective
|
850
|
+
texts.
|
851
|
+
|
852
|
+
Shortcuts such as `key: "Control+o"` or `key: "Control+Shift+T"` are supported as well. When specified with the
|
853
|
+
modifier, modifier is pressed and being held while the subsequent key is being pressed.
|
854
|
+
|
855
|
+
```python sync title=example_aa4598bd7dbeb8d2f8f5c0aa3bdc84042eb396de37b49f8ff8c1ea39f080f709.py
|
856
|
+
page = browser.new_page()
|
857
|
+
page.goto("https://keycode.info")
|
858
|
+
page.press("body", "A")
|
859
|
+
page.screenshot(path="a.png")
|
860
|
+
page.press("body", "ArrowLeft")
|
861
|
+
page.screenshot(path="arrow_left.png")
|
862
|
+
page.press("body", "Shift+O")
|
863
|
+
page.screenshot(path="o.png")
|
864
|
+
browser.close()
|
865
|
+
|
866
|
+
```
|
867
|
+
|
868
|
+
|
869
|
+
|
870
|
+
## query_selector
|
871
|
+
|
872
|
+
```
|
873
|
+
def query_selector(selector)
|
874
|
+
```
|
875
|
+
|
876
|
+
The method finds an element matching the specified selector within the page. If no elements match the selector, the
|
877
|
+
return value resolves to `null`. To wait for an element on the page, use [Page#wait_for_selector](./page#wait_for_selector).
|
878
|
+
|
879
|
+
Shortcut for main frame's [Frame#query_selector](./frame#query_selector).
|
880
|
+
|
881
|
+
## query_selector_all
|
882
|
+
|
883
|
+
```
|
884
|
+
def query_selector_all(selector)
|
885
|
+
```
|
886
|
+
|
887
|
+
The method finds all elements matching the specified selector within the page. If no elements match the selector, the
|
888
|
+
return value resolves to `[]`.
|
889
|
+
|
890
|
+
Shortcut for main frame's [Frame#query_selector_all](./frame#query_selector_all).
|
891
|
+
|
892
|
+
## reload
|
893
|
+
|
894
|
+
```
|
895
|
+
def reload(timeout: nil, waitUntil: nil)
|
896
|
+
```
|
897
|
+
|
898
|
+
Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the
|
899
|
+
last redirect.
|
900
|
+
|
901
|
+
## route
|
902
|
+
|
903
|
+
```
|
904
|
+
def route(url, handler)
|
905
|
+
```
|
906
|
+
|
907
|
+
Routing provides the capability to modify network requests that are made by a page.
|
908
|
+
|
909
|
+
Once routing is enabled, every request matching the url pattern will stall unless it's continued, fulfilled or aborted.
|
910
|
+
|
911
|
+
> NOTE: The handler will only be called for the first url if the response is a redirect.
|
912
|
+
|
913
|
+
An example of a naive handler that aborts all image requests:
|
914
|
+
|
915
|
+
```python sync title=example_a3038a6fd55b06cb841251877bf6eb781b08018695514c6e0054848d4e93d345.py
|
916
|
+
page = browser.new_page()
|
917
|
+
page.route("**/*.{png,jpg,jpeg}", lambda route: route.abort())
|
918
|
+
page.goto("https://example.com")
|
919
|
+
browser.close()
|
920
|
+
|
921
|
+
```
|
922
|
+
|
923
|
+
or the same snippet using a regex pattern instead:
|
924
|
+
|
925
|
+
```python sync title=example_7fda2a761bdd66b942415ab444c6b4bb89dd87ec0f0a4a03e6775feb694f7913.py
|
926
|
+
page = browser.new_page()
|
927
|
+
page.route(re.compile(r"(\.png$)|(\.jpg$)"), lambda route: route.abort())
|
928
|
+
page.goto("https://example.com")
|
929
|
+
browser.close()
|
930
|
+
|
931
|
+
```
|
932
|
+
|
933
|
+
It is possible to examine the request to decide the route action. For example, mocking all requests that contain some
|
934
|
+
post data, and leaving all other requests as is:
|
935
|
+
|
936
|
+
```python sync title=example_ff4fba1273c7e65f4d68b4fcdd9dc4b792bba435005f0b9e7066ca18ded750b5.py
|
937
|
+
def handle_route(route):
|
938
|
+
if ("my-string" in route.request.post_data)
|
939
|
+
route.fulfill(body="mocked-data")
|
940
|
+
else
|
941
|
+
route.continue_()
|
942
|
+
page.route("/api/**", handle_route)
|
943
|
+
|
944
|
+
```
|
945
|
+
|
946
|
+
Page routes take precedence over browser context routes (set up with [BrowserContext#route](./browser_context#route)) when request
|
947
|
+
matches both handlers.
|
948
|
+
|
949
|
+
To remove a route with its handler you can use [Page#unroute](./page#unroute).
|
950
|
+
|
951
|
+
> NOTE: Enabling routing disables http cache.
|
952
|
+
|
953
|
+
## screenshot
|
954
|
+
|
955
|
+
```
|
956
|
+
def screenshot(
|
957
|
+
clip: nil,
|
958
|
+
fullPage: nil,
|
959
|
+
omitBackground: nil,
|
960
|
+
path: nil,
|
961
|
+
quality: nil,
|
962
|
+
timeout: nil,
|
963
|
+
type: nil)
|
964
|
+
```
|
965
|
+
|
966
|
+
Returns the buffer with the captured screenshot.
|
967
|
+
|
968
|
+
## select_option
|
969
|
+
|
970
|
+
```
|
971
|
+
def select_option(
|
972
|
+
selector,
|
973
|
+
element: nil,
|
974
|
+
index: nil,
|
975
|
+
value: nil,
|
976
|
+
label: nil,
|
977
|
+
noWaitAfter: nil,
|
978
|
+
timeout: nil)
|
979
|
+
```
|
980
|
+
|
981
|
+
This method waits for an element matching `selector`, waits for [actionability](https://playwright.dev/python/docs/actionability) checks, waits until
|
982
|
+
all specified options are present in the `<select>` element and selects these options.
|
983
|
+
|
984
|
+
If the target element is not a `<select>` element, this method throws an error. However, if the element is inside the
|
985
|
+
`<label>` element that has an associated
|
986
|
+
[control](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement/control), the control will be used instead.
|
987
|
+
|
988
|
+
Returns the array of option values that have been successfully selected.
|
989
|
+
|
990
|
+
Triggers a `change` and `input` event once all the provided options have been selected.
|
991
|
+
|
992
|
+
```python sync title=example_4b17eb65721c55859c50eb12b4ee762e65408618cf3b7d07958b68d60ea6be6c.py
|
993
|
+
# single selection matching the value
|
994
|
+
page.select_option("select#colors", "blue")
|
995
|
+
# single selection matching both the label
|
996
|
+
page.select_option("select#colors", label="blue")
|
997
|
+
# multiple selection
|
998
|
+
page.select_option("select#colors", value=["red", "green", "blue"])
|
999
|
+
|
1000
|
+
```
|
1001
|
+
|
1002
|
+
Shortcut for main frame's [Frame#select_option](./frame#select_option).
|
1003
|
+
|
1004
|
+
## set_content
|
1005
|
+
|
1006
|
+
```
|
1007
|
+
def set_content(html, timeout: nil, waitUntil: nil)
|
1008
|
+
```
|
1009
|
+
alias: `content=`
|
1010
|
+
|
1011
|
+
|
1012
|
+
|
1013
|
+
## set_default_navigation_timeout
|
1014
|
+
|
1015
|
+
```
|
1016
|
+
def set_default_navigation_timeout(timeout)
|
1017
|
+
```
|
1018
|
+
alias: `default_navigation_timeout=`
|
1019
|
+
|
1020
|
+
This setting will change the default maximum navigation time for the following methods and related shortcuts:
|
1021
|
+
- [Page#go_back](./page#go_back)
|
1022
|
+
- [Page#go_forward](./page#go_forward)
|
1023
|
+
- [Page#goto](./page#goto)
|
1024
|
+
- [Page#reload](./page#reload)
|
1025
|
+
- [Page#set_content](./page#set_content)
|
1026
|
+
- [Page#expect_navigation](./page#expect_navigation)
|
1027
|
+
- [Page#wait_for_url](./page#wait_for_url)
|
1028
|
+
|
1029
|
+
> NOTE: [Page#set_default_navigation_timeout](./page#set_default_navigation_timeout) takes priority over [Page#set_default_timeout](./page#set_default_timeout),
|
1030
|
+
[BrowserContext#set_default_timeout](./browser_context#set_default_timeout) and [BrowserContext#set_default_navigation_timeout](./browser_context#set_default_navigation_timeout).
|
1031
|
+
|
1032
|
+
## set_default_timeout
|
1033
|
+
|
1034
|
+
```
|
1035
|
+
def set_default_timeout(timeout)
|
1036
|
+
```
|
1037
|
+
alias: `default_timeout=`
|
1038
|
+
|
1039
|
+
This setting will change the default maximum time for all the methods accepting `timeout` option.
|
1040
|
+
|
1041
|
+
> NOTE: [Page#set_default_navigation_timeout](./page#set_default_navigation_timeout) takes priority over [Page#set_default_timeout](./page#set_default_timeout).
|
1042
|
+
|
1043
|
+
## set_extra_http_headers
|
1044
|
+
|
1045
|
+
```
|
1046
|
+
def set_extra_http_headers(headers)
|
1047
|
+
```
|
1048
|
+
alias: `extra_http_headers=`
|
1049
|
+
|
1050
|
+
The extra HTTP headers will be sent with every request the page initiates.
|
1051
|
+
|
1052
|
+
> NOTE: [Page#set_extra_http_headers](./page#set_extra_http_headers) does not guarantee the order of headers in the outgoing requests.
|
1053
|
+
|
1054
|
+
## set_input_files
|
1055
|
+
|
1056
|
+
```
|
1057
|
+
def set_input_files(selector, files, noWaitAfter: nil, timeout: nil)
|
1058
|
+
```
|
1059
|
+
|
1060
|
+
This method expects `selector` to point to an
|
1061
|
+
[input element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input).
|
1062
|
+
|
1063
|
+
Sets the value of the file input to these file paths or files. If some of the `filePaths` are relative paths, then they
|
1064
|
+
are resolved relative to the the current working directory. For empty array, clears the selected files.
|
1065
|
+
|
1066
|
+
## set_viewport_size
|
1067
|
+
|
1068
|
+
```
|
1069
|
+
def set_viewport_size(viewportSize)
|
1070
|
+
```
|
1071
|
+
alias: `viewport_size=`
|
1072
|
+
|
1073
|
+
In the case of multiple pages in a single browser, each page can have its own viewport size. However,
|
1074
|
+
[Browser#new_context](./browser#new_context) allows to set viewport size (and more) for all pages in the context at once.
|
1075
|
+
|
1076
|
+
`page.setViewportSize` will resize the page. A lot of websites don't expect phones to change size, so you should set the
|
1077
|
+
viewport size before navigating to the page.
|
1078
|
+
|
1079
|
+
```python sync title=example_e3883d51c0785c34b62633fe311c4f1252dd9f29e6b4b6c7719f1eb74384e6e9.py
|
1080
|
+
page = browser.new_page()
|
1081
|
+
page.set_viewport_size({"width": 640, "height": 480})
|
1082
|
+
page.goto("https://example.com")
|
1083
|
+
|
1084
|
+
```
|
1085
|
+
|
1086
|
+
|
1087
|
+
|
1088
|
+
## tap_point
|
1089
|
+
|
1090
|
+
```
|
1091
|
+
def tap_point(
|
1092
|
+
selector,
|
1093
|
+
force: nil,
|
1094
|
+
modifiers: nil,
|
1095
|
+
noWaitAfter: nil,
|
1096
|
+
position: nil,
|
1097
|
+
timeout: nil,
|
1098
|
+
trial: nil)
|
1099
|
+
```
|
1100
|
+
|
1101
|
+
This method taps an element matching `selector` by performing the following steps:
|
1102
|
+
1. Find an element matching `selector`. If there is none, wait until a matching element is attached to the DOM.
|
1103
|
+
1. Wait for [actionability](https://playwright.dev/python/docs/actionability) checks on the matched element, unless `force` option is set. If the
|
1104
|
+
element is detached during the checks, the whole action is retried.
|
1105
|
+
1. Scroll the element into view if needed.
|
1106
|
+
1. Use [Page#touchscreen](./page#touchscreen) to tap the center of the element, or the specified `position`.
|
1107
|
+
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
|
1108
|
+
|
1109
|
+
When all steps combined have not finished during the specified `timeout`, this method throws a `TimeoutError`. Passing
|
1110
|
+
zero timeout disables this.
|
1111
|
+
|
1112
|
+
> NOTE: [Page#tap_point](./page#tap_point) requires that the `hasTouch` option of the browser context be set to true.
|
1113
|
+
|
1114
|
+
Shortcut for main frame's [Frame#tap_point](./frame#tap_point).
|
1115
|
+
|
1116
|
+
## text_content
|
1117
|
+
|
1118
|
+
```
|
1119
|
+
def text_content(selector, timeout: nil)
|
1120
|
+
```
|
1121
|
+
|
1122
|
+
Returns `element.textContent`.
|
1123
|
+
|
1124
|
+
## title
|
1125
|
+
|
1126
|
+
```
|
1127
|
+
def title
|
1128
|
+
```
|
1129
|
+
|
1130
|
+
Returns the page's title. Shortcut for main frame's [Frame#title](./frame#title).
|
1131
|
+
|
1132
|
+
## type
|
1133
|
+
|
1134
|
+
```
|
1135
|
+
def type(
|
1136
|
+
selector,
|
1137
|
+
text,
|
1138
|
+
delay: nil,
|
1139
|
+
noWaitAfter: nil,
|
1140
|
+
timeout: nil)
|
1141
|
+
```
|
1142
|
+
|
1143
|
+
Sends a `keydown`, `keypress`/`input`, and `keyup` event for each character in the text. `page.type` can be used to send
|
1144
|
+
fine-grained keyboard events. To fill values in form fields, use [Page#fill](./page#fill).
|
1145
|
+
|
1146
|
+
To press a special key, like `Control` or `ArrowDown`, use [Keyboard#press](./keyboard#press).
|
1147
|
+
|
1148
|
+
```python sync title=example_4c7291f6023d2fe4f957cb7727646b50fdee40275db330a6f4517e349ea7f916.py
|
1149
|
+
page.type("#mytextarea", "hello") # types instantly
|
1150
|
+
page.type("#mytextarea", "world", delay=100) # types slower, like a user
|
1151
|
+
|
1152
|
+
```
|
1153
|
+
|
1154
|
+
Shortcut for main frame's [Frame#type](./frame#type).
|
1155
|
+
|
1156
|
+
## uncheck
|
1157
|
+
|
1158
|
+
```
|
1159
|
+
def uncheck(
|
1160
|
+
selector,
|
1161
|
+
force: nil,
|
1162
|
+
noWaitAfter: nil,
|
1163
|
+
position: nil,
|
1164
|
+
timeout: nil,
|
1165
|
+
trial: nil)
|
1166
|
+
```
|
1167
|
+
|
1168
|
+
This method unchecks an element matching `selector` by performing the following steps:
|
1169
|
+
1. Find an element matching `selector`. If there is none, wait until a matching element is attached to the DOM.
|
1170
|
+
1. Ensure that matched element is a checkbox or a radio input. If not, this method throws. If the element is already
|
1171
|
+
unchecked, this method returns immediately.
|
1172
|
+
1. Wait for [actionability](https://playwright.dev/python/docs/actionability) checks on the matched element, unless `force` option is set. If the
|
1173
|
+
element is detached during the checks, the whole action is retried.
|
1174
|
+
1. Scroll the element into view if needed.
|
1175
|
+
1. Use [Page#mouse](./page#mouse) to click in the center of the element.
|
1176
|
+
1. Wait for initiated navigations to either succeed or fail, unless `noWaitAfter` option is set.
|
1177
|
+
1. Ensure that the element is now unchecked. If not, this method throws.
|
1178
|
+
|
1179
|
+
When all steps combined have not finished during the specified `timeout`, this method throws a `TimeoutError`. Passing
|
1180
|
+
zero timeout disables this.
|
1181
|
+
|
1182
|
+
Shortcut for main frame's [Frame#uncheck](./frame#uncheck).
|
1183
|
+
|
1184
|
+
## unroute
|
1185
|
+
|
1186
|
+
```
|
1187
|
+
def unroute(url, handler: nil)
|
1188
|
+
```
|
1189
|
+
|
1190
|
+
Removes a route created with [Page#route](./page#route). When `handler` is not specified, removes all routes for the `url`.
|
1191
|
+
|
1192
|
+
## url
|
1193
|
+
|
1194
|
+
```
|
1195
|
+
def url
|
1196
|
+
```
|
1197
|
+
|
1198
|
+
Shortcut for main frame's [Frame#url](./frame#url).
|
1199
|
+
|
1200
|
+
## video
|
1201
|
+
|
1202
|
+
```
|
1203
|
+
def video
|
1204
|
+
```
|
1205
|
+
|
1206
|
+
Video object associated with this page.
|
1207
|
+
|
1208
|
+
## viewport_size
|
1209
|
+
|
1210
|
+
```
|
1211
|
+
def viewport_size
|
1212
|
+
```
|
1213
|
+
|
1214
|
+
|
1215
|
+
|
1216
|
+
## expect_console_message
|
1217
|
+
|
1218
|
+
```
|
1219
|
+
def expect_console_message(predicate: nil, timeout: nil, &block)
|
1220
|
+
```
|
1221
|
+
|
1222
|
+
Performs action and waits for a [ConsoleMessage](./console_message) to be logged by in the page. If predicate is provided, it passes
|
1223
|
+
[ConsoleMessage](./console_message) value into the `predicate` function and waits for `predicate(message)` to return a truthy value. Will
|
1224
|
+
throw an error if the page is closed before the console event is fired.
|
1225
|
+
|
1226
|
+
## expect_download
|
1227
|
+
|
1228
|
+
```
|
1229
|
+
def expect_download(predicate: nil, timeout: nil, &block)
|
1230
|
+
```
|
1231
|
+
|
1232
|
+
Performs action and waits for a new `Download`. If predicate is provided, it passes `Download` value into the
|
1233
|
+
`predicate` function and waits for `predicate(download)` to return a truthy value. Will throw an error if the page is
|
1234
|
+
closed before the download event is fired.
|
1235
|
+
|
1236
|
+
## expect_event
|
1237
|
+
|
1238
|
+
```
|
1239
|
+
def expect_event(event, predicate: nil, timeout: nil, &block)
|
1240
|
+
```
|
1241
|
+
|
1242
|
+
Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy
|
1243
|
+
value. Will throw an error if the page is closed before the event is fired. Returns the event data value.
|
1244
|
+
|
1245
|
+
```python sync title=example_1b007e0db5f2b594b586367be3b56f9eb9b928740efbceada2c60cb7794592d4.py
|
1246
|
+
with page.expect_event("framenavigated") as event_info:
|
1247
|
+
page.click("button")
|
1248
|
+
frame = event_info.value
|
1249
|
+
|
1250
|
+
```
|
1251
|
+
|
1252
|
+
|
1253
|
+
|
1254
|
+
## expect_file_chooser
|
1255
|
+
|
1256
|
+
```
|
1257
|
+
def expect_file_chooser(predicate: nil, timeout: nil, &block)
|
1258
|
+
```
|
1259
|
+
|
1260
|
+
Performs action and waits for a new [FileChooser](./file_chooser) to be created. If predicate is provided, it passes [FileChooser](./file_chooser) value
|
1261
|
+
into the `predicate` function and waits for `predicate(fileChooser)` to return a truthy value. Will throw an error if
|
1262
|
+
the page is closed before the file chooser is opened.
|
1263
|
+
|
1264
|
+
## wait_for_function
|
1265
|
+
|
1266
|
+
```
|
1267
|
+
def wait_for_function(expression, arg: nil, polling: nil, timeout: nil)
|
1268
|
+
```
|
1269
|
+
|
1270
|
+
Returns when the `expression` returns a truthy value. It resolves to a JSHandle of the truthy value.
|
1271
|
+
|
1272
|
+
The [Page#wait_for_function](./page#wait_for_function) can be used to observe viewport size change:
|
1273
|
+
|
1274
|
+
```python sync title=example_e50869c913bec2f0a89a22ff1c438128c3c8f2e3710acb10665445cf52e3ec73.py
|
1275
|
+
from playwright.sync_api import sync_playwright
|
1276
|
+
|
1277
|
+
def run(playwright):
|
1278
|
+
webkit = playwright.webkit
|
1279
|
+
browser = webkit.launch()
|
1280
|
+
page = browser.new_page()
|
1281
|
+
page.evaluate("window.x = 0; setTimeout(() => { window.x = 100 }, 1000);")
|
1282
|
+
page.wait_for_function("() => window.x > 0")
|
1283
|
+
browser.close()
|
1284
|
+
|
1285
|
+
with sync_playwright() as playwright:
|
1286
|
+
run(playwright)
|
1287
|
+
|
1288
|
+
```
|
1289
|
+
|
1290
|
+
To pass an argument to the predicate of [Page#wait_for_function](./page#wait_for_function) function:
|
1291
|
+
|
1292
|
+
```python sync title=example_04c93558dde8de62944515a8ed91fda6e0d01feca4d3bb2e58c6fda10a8c6ade.py
|
1293
|
+
selector = ".foo"
|
1294
|
+
page.wait_for_function("selector => !!document.querySelector(selector)", selector)
|
1295
|
+
|
1296
|
+
```
|
1297
|
+
|
1298
|
+
Shortcut for main frame's [Frame#wait_for_function](./frame#wait_for_function).
|
1299
|
+
|
1300
|
+
## wait_for_load_state
|
1301
|
+
|
1302
|
+
```
|
1303
|
+
def wait_for_load_state(state: nil, timeout: nil)
|
1304
|
+
```
|
1305
|
+
|
1306
|
+
Returns when the required load state has been reached.
|
1307
|
+
|
1308
|
+
This resolves when the page reaches a required load state, `load` by default. The navigation must have been committed
|
1309
|
+
when this method is called. If current document has already reached the required state, resolves immediately.
|
1310
|
+
|
1311
|
+
```python sync title=example_cd35fb085612055231ddf97f68bc5331b4620914e0686b889f2cd4061836cff8.py
|
1312
|
+
page.click("button") # click triggers navigation.
|
1313
|
+
page.wait_for_load_state() # the promise resolves after "load" event.
|
1314
|
+
|
1315
|
+
```
|
1316
|
+
|
1317
|
+
```python sync title=example_51ba8a745d5093516e9a50482d8bf3ce29afe507ca5cfe89f4a0e35963f52a36.py
|
1318
|
+
with page.expect_popup() as page_info:
|
1319
|
+
page.click("button") # click triggers a popup.
|
1320
|
+
popup = page_info.value
|
1321
|
+
# Following resolves after "domcontentloaded" event.
|
1322
|
+
popup.wait_for_load_state("domcontentloaded")
|
1323
|
+
print(popup.title()) # popup is ready to use.
|
1324
|
+
|
1325
|
+
```
|
1326
|
+
|
1327
|
+
Shortcut for main frame's [Frame#wait_for_load_state](./frame#wait_for_load_state).
|
1328
|
+
|
1329
|
+
## expect_navigation
|
1330
|
+
|
1331
|
+
```
|
1332
|
+
def expect_navigation(timeout: nil, url: nil, waitUntil: nil, &block)
|
1333
|
+
```
|
1334
|
+
|
1335
|
+
Waits for the main frame navigation and returns the main resource response. In case of multiple redirects, the
|
1336
|
+
navigation will resolve with the response of the last redirect. In case of navigation to a different anchor or
|
1337
|
+
navigation due to History API usage, the navigation will resolve with `null`.
|
1338
|
+
|
1339
|
+
This resolves when the page navigates to a new URL or reloads. It is useful for when you run code which will indirectly
|
1340
|
+
cause the page to navigate. e.g. The click target has an `onclick` handler that triggers navigation from a `setTimeout`.
|
1341
|
+
Consider this example:
|
1342
|
+
|
1343
|
+
```python sync title=example_bc5a01f756c1275b9942c4b3e50a9f1748c04da8d5f8f697567b9d04806ec0dc.py
|
1344
|
+
with page.expect_navigation():
|
1345
|
+
page.click("a.delayed-navigation") # clicking the link will indirectly cause a navigation
|
1346
|
+
# Resolves after navigation has finished
|
1347
|
+
|
1348
|
+
```
|
1349
|
+
|
1350
|
+
> NOTE: Usage of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to change the URL is
|
1351
|
+
considered a navigation.
|
1352
|
+
|
1353
|
+
Shortcut for main frame's [`method: Frame.waitForNavigation`].
|
1354
|
+
|
1355
|
+
## expect_popup
|
1356
|
+
|
1357
|
+
```
|
1358
|
+
def expect_popup(predicate: nil, timeout: nil, &block)
|
1359
|
+
```
|
1360
|
+
|
1361
|
+
Performs action and waits for a popup [Page](./page). If predicate is provided, it passes [Popup] value into the `predicate`
|
1362
|
+
function and waits for `predicate(page)` to return a truthy value. Will throw an error if the page is closed before the
|
1363
|
+
popup event is fired.
|
1364
|
+
|
1365
|
+
## expect_request
|
1366
|
+
|
1367
|
+
```
|
1368
|
+
def expect_request(urlOrPredicate, timeout: nil)
|
1369
|
+
```
|
1370
|
+
|
1371
|
+
Waits for the matching request and returns it. See [waiting for event](https://playwright.dev/python/docs/events) for more details
|
1372
|
+
about events.
|
1373
|
+
|
1374
|
+
```python sync title=example_9246912bc386c2f9310662279b12200ae131f724a1ec1ca99e511568767cb9c8.py
|
1375
|
+
with page.expect_request("http://example.com/resource") as first:
|
1376
|
+
page.click('button')
|
1377
|
+
first_request = first.value
|
1378
|
+
|
1379
|
+
# or with a lambda
|
1380
|
+
with page.expect_request(lambda request: request.url == "http://example.com" and request.method == "get") as second:
|
1381
|
+
page.click('img')
|
1382
|
+
second_request = second.value
|
1383
|
+
|
1384
|
+
```
|
1385
|
+
|
1386
|
+
|
1387
|
+
|
1388
|
+
## expect_response
|
1389
|
+
|
1390
|
+
```
|
1391
|
+
def expect_response(urlOrPredicate, timeout: nil)
|
1392
|
+
```
|
1393
|
+
|
1394
|
+
Returns the matched response. See [waiting for event](https://playwright.dev/python/docs/events) for more details about events.
|
1395
|
+
|
1396
|
+
```python sync title=example_d2a76790c0bb59bf5ae2f41d1a29b50954412136de3699ec79dc33cdfd56004b.py
|
1397
|
+
with page.expect_response("https://example.com/resource") as response_info:
|
1398
|
+
page.click("input")
|
1399
|
+
response = response_info.value
|
1400
|
+
return response.ok
|
1401
|
+
|
1402
|
+
# or with a lambda
|
1403
|
+
with page.expect_response(lambda response: response.url == "https://example.com" and response.status === 200) as response_info:
|
1404
|
+
page.click("input")
|
1405
|
+
response = response_info.value
|
1406
|
+
return response.ok
|
1407
|
+
|
1408
|
+
```
|
1409
|
+
|
1410
|
+
|
1411
|
+
|
1412
|
+
## wait_for_selector
|
1413
|
+
|
1414
|
+
```
|
1415
|
+
def wait_for_selector(selector, state: nil, timeout: nil)
|
1416
|
+
```
|
1417
|
+
|
1418
|
+
Returns when element specified by selector satisfies `state` option. Returns `null` if waiting for `hidden` or
|
1419
|
+
`detached`.
|
1420
|
+
|
1421
|
+
Wait for the `selector` to satisfy `state` option (either appear/disappear from dom, or become visible/hidden). If at
|
1422
|
+
the moment of calling the method `selector` already satisfies the condition, the method will return immediately. If the
|
1423
|
+
selector doesn't satisfy the condition for the `timeout` milliseconds, the function will throw.
|
1424
|
+
|
1425
|
+
This method works across navigations:
|
1426
|
+
|
1427
|
+
```python sync title=example_0a62ff34b0d31a64dd1597b9dff456e4139b36207d26efdec7109e278dc315a3.py
|
1428
|
+
from playwright.sync_api import sync_playwright
|
1429
|
+
|
1430
|
+
def run(playwright):
|
1431
|
+
chromium = playwright.chromium
|
1432
|
+
browser = chromium.launch()
|
1433
|
+
page = browser.new_page()
|
1434
|
+
for current_url in ["https://google.com", "https://bbc.com"]:
|
1435
|
+
page.goto(current_url, wait_until="domcontentloaded")
|
1436
|
+
element = page.wait_for_selector("img")
|
1437
|
+
print("Loaded image: " + str(element.get_attribute("src")))
|
1438
|
+
browser.close()
|
1439
|
+
|
1440
|
+
with sync_playwright() as playwright:
|
1441
|
+
run(playwright)
|
1442
|
+
|
1443
|
+
```
|
1444
|
+
|
1445
|
+
|
1446
|
+
|
1447
|
+
## wait_for_url
|
1448
|
+
|
1449
|
+
```
|
1450
|
+
def wait_for_url(url, timeout: nil, waitUntil: nil)
|
1451
|
+
```
|
1452
|
+
|
1453
|
+
Waits for the main frame to navigate to the given URL.
|
1454
|
+
|
1455
|
+
```python sync title=example_a49b1deed2b93fe358b57bca9c4032f44b3d24436a78720421ba040aad4d661c.py
|
1456
|
+
page.click("a.delayed-navigation") # clicking the link will indirectly cause a navigation
|
1457
|
+
page.wait_for_url("**/target.html")
|
1458
|
+
|
1459
|
+
```
|
1460
|
+
|
1461
|
+
Shortcut for main frame's [Frame#wait_for_url](./frame#wait_for_url).
|
1462
|
+
|
1463
|
+
## accessibility
|
1464
|
+
|
1465
|
+
## keyboard
|
1466
|
+
|
1467
|
+
## mouse
|
1468
|
+
|
1469
|
+
## touchscreen
|