1
1
# 🎭 [ Playwright] ( https://playwright.dev ) for Python [ ![ PyPI version] ( https://badge.fury.io/py/playwright.svg )] ( https://pypi.python.org/pypi/playwright/ ) [ ![ Anaconda version] ( https://img.shields.io/conda/v/microsoft/playwright )] ( https://anaconda.org/Microsoft/playwright ) [ ![ Join Slack] ( https://img.shields.io/badge/join-slack-infomational )] ( https://aka.ms/playwright-slack )
2
2
3
- #### [ Docs] ( https://playwright.dev/python/docs/intro ) | [ API] ( https://playwright.dev/python/docs/api/class-playwright )
4
-
5
3
Playwright is a Python library to automate [ Chromium] ( https://www.chromium.org/Home ) , [ Firefox] ( https://www.mozilla.org/en-US/firefox/new/ ) and [ WebKit] ( https://webkit.org/ ) browsers with a single API. Playwright delivers automation that is ** ever-green** , ** capable** , ** reliable** and ** fast** . [ See how Playwright is better] ( https://playwright.dev/python/docs/why-playwright ) .
6
4
7
5
| | Linux | macOS | Windows |
@@ -10,60 +8,14 @@ Playwright is a Python library to automate [Chromium](https://www.chromium.org/H
10
8
| WebKit <!-- GEN:webkit-version --> 14.2<!-- GEN:stop --> | ✅ | ✅ | ✅ |
11
9
| Firefox <!-- GEN:firefox-version --> 89.0<!-- GEN:stop --> | ✅ | ✅ | ✅ |
12
10
13
- Headless execution is supported for all browsers on all platforms.
14
-
15
- - [ Usage] ( #usage )
16
- - [ Record and generate code] ( #record-and-generate-code )
17
- - [ Sync API] ( #sync-api )
18
- - [ Async API] ( #async-api )
19
- - [ With pytest] ( #with-pytest )
20
- - [ Interactive mode (REPL)] ( #interactive-mode-repl )
21
- - [ Examples] ( #examples )
22
- - [ Mobile and geolocation] ( #mobile-and-geolocation )
23
- - [ Evaluate JS in browser] ( #evaluate-js-in-browser )
24
- - [ Intercept network requests] ( #intercept-network-requests )
25
- - [ Known issues] ( #known-issues )
26
- - [ Documentation] ( #documentation )
27
-
28
- ## Usage - pip
29
-
30
- [ ![ PyPI version] ( https://badge.fury.io/py/playwright.svg )] ( https://pypi.python.org/pypi/playwright/ )
31
-
32
- ``` sh
33
- pip install playwright
34
- playwright install
35
- ```
36
-
37
- This installs Playwright and browser binaries for Chromium, Firefox and WebKit. Playwright requires Python 3.7+.
38
-
39
- ## Usage - conda
40
-
41
- [ ![ Anaconda version] ( https://img.shields.io/conda/v/microsoft/playwright )] ( https://anaconda.org/Microsoft/playwright )
42
-
43
- ``` sh
44
- conda config --add channels conda-forge
45
- conda config --add channels microsoft
46
- conda install playwright
47
- playwright install
48
- ```
49
-
50
- This installs Playwright and browser binaries for Chromium, Firefox and WebKit with the conda package manager. Playwright requires a conda environment with Python 3.7+.
51
-
52
- #### Record and generate code
53
-
54
- Playwright can record user interactions in a browser and generate code. [ See demo] ( https://user-images.githubusercontent.com/284612/95930164-ad52fb80-0d7a-11eb-852d-04bfd19de800.gif ) .
55
-
56
- ``` sh
57
- # Pass --help to see all options
58
- playwright codegen
59
- ```
11
+ ## Documentation
60
12
61
- Playwright offers both sync (blocking) API and async API. They are identical in terms of capabilities and only differ in how one consumes the API.
13
+ [ https://playwright.dev/dotnet/docs/intro ] ( https://playwright.dev/dotnet/docs/intro )
62
14
63
- #### Sync API
15
+ ## API Reference
16
+ [ https://playwright.dev/dotnet/docs/api/class-playwright ] ( https://playwright.dev/dotnet/docs/api/class-playwright )
64
17
65
- This is our default API for short snippets and tests. If you are not using asyncio in your
66
- application, it is the easiest to use Sync API notation.
18
+ ## Example
67
19
68
20
``` py
69
21
from playwright.sync_api import sync_playwright
@@ -77,15 +29,6 @@ with sync_playwright() as p:
77
29
browser.close()
78
30
```
79
31
80
- #### Async API
81
-
82
- If your app is based on the modern asyncio loop and you are used to async/await constructs,
83
- Playwright exposes Async API for you. You should use this API inside a Python REPL supporting ` asyncio ` like with ` python -m asyncio `
84
-
85
- ``` console
86
- python -m asyncio
87
- ```
88
-
89
32
``` py
90
33
import asyncio
91
34
from playwright.async_api import async_playwright
@@ -102,209 +45,9 @@ async def main():
102
45
asyncio.run(main())
103
46
```
104
47
105
- #### With pytest
106
-
107
- Use our [ pytest plugin for Playwright] ( https://github.com/microsoft/playwright-pytest#readme ) .
108
-
109
- ``` py
110
- def test_playwright_is_visible_on_google (page ):
111
- page.goto(" https://www.google.com" )
112
- page.type(" input[name=q]" , " Playwright GitHub" )
113
- page.click(" input[type=submit]" )
114
- page.wait_for_selector(" text=microsoft/Playwright" )
115
- ```
116
-
117
- #### Interactive mode (REPL)
118
-
119
- Blocking REPL, as in CLI:
120
-
121
- ``` py
122
- >> > from playwright.sync_api import sync_playwright
123
- >> > playwright = sync_playwright().start()
124
-
125
- # Use playwright.chromium, playwright.firefox or playwright.webkit
126
- # Pass headless=False to see the browser UI
127
- >> > browser = playwright.chromium.launch()
128
- >> > page = browser.new_page()
129
- >> > page.goto(" http://whatsmyuseragent.org/" )
130
- >> > page.screenshot(path = " example.png" )
131
- >> > browser.close()
132
- >> > playwright.stop()
133
- ```
134
-
135
- Async REPL such as ` asyncio ` REPL:
136
-
137
- ``` console
138
- python -m asyncio
139
- ```
140
-
141
- ``` py
142
- >> > from playwright.async_api import async_playwright
143
- >> > playwright = await async_playwright().start()
144
- >> > browser = await playwright.chromium.launch()
145
- >> > page = await browser.new_page()
146
- >> > await page.goto(" http://whatsmyuseragent.org/" )
147
- >> > await page.screenshot(path = " example.png" )
148
- >> > await browser.close()
149
- >> > await playwright.stop()
150
- ```
151
-
152
- ## Examples
153
-
154
- #### Mobile and geolocation
155
-
156
- This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.
157
-
158
- ``` py
159
- from playwright.sync_api import sync_playwright
160
-
161
- with sync_playwright() as p:
162
- iphone_11 = p.devices[" iPhone 11 Pro" ]
163
- browser = p.webkit.launch(headless = False )
164
- context = browser.new_context(
165
- ** iphone_11,
166
- locale = " en-US" ,
167
- geolocation = {" longitude" : 12.492507 , " latitude" : 41.889938 },
168
- permissions = [" geolocation" ]
169
- )
170
- page = context.new_page()
171
- page.goto(" https://maps.google.com" )
172
- page.click(" text=Your location" )
173
- page.screenshot(path = " colosseum-iphone.png" )
174
- browser.close()
175
- ```
176
-
177
- <details >
178
- <summary >Async variant</summary >
179
-
180
- ``` py
181
- import asyncio
182
- from playwright.async_api import async_playwright
183
-
184
- async def main ():
185
- async with async_playwright() as p:
186
- iphone_11 = p.devices[" iPhone 11 Pro" ]
187
- browser = await p.webkit.launch(headless = False )
188
- context = await browser.new_context(
189
- ** iphone_11,
190
- locale = " en-US" ,
191
- geolocation = {" longitude" : 12.492507 , " latitude" : 41.889938 },
192
- permissions = [" geolocation" ]
193
- )
194
- page = await context.new_page()
195
- await page.goto(" https://maps.google.com" )
196
- await page.click(" text=" Your location" " )
197
- await page.screenshot(path = " colosseum-iphone.png" )
198
- await browser.close()
199
-
200
- asyncio.run(main())
201
- ```
202
-
203
- </details >
204
-
205
- #### Evaluate JS in browser
206
-
207
- This code snippet navigates to example.com in Firefox, and executes a script in the page context.
208
-
209
- ``` py
210
- from playwright.sync_api import sync_playwright
211
-
212
- with sync_playwright() as p:
213
- browser = p.firefox.launch()
214
- page = browser.new_page()
215
- page.goto(" https://www.example.com/" )
216
- dimensions = page.evaluate(""" () => {
217
- return {
218
- width: document.documentElement.clientWidth,
219
- height: document.documentElement.clientHeight,
220
- deviceScaleFactor: window.devicePixelRatio
221
- }
222
- }""" )
223
- print (dimensions)
224
- browser.close()
225
- ```
226
-
227
- <details >
228
- <summary >Async variant</summary >
229
-
230
- ``` py
231
- import asyncio
232
- from playwright.async_api import async_playwright
233
-
234
- async def main ():
235
- async with async_playwright() as p:
236
- browser = await p.firefox.launch()
237
- page = await browser.new_page()
238
- await page.goto(" https://www.example.com/" )
239
- dimensions = await page.evaluate(""" () => {
240
- return {
241
- width: document.documentElement.clientWidth,
242
- height: document.documentElement.clientHeight,
243
- deviceScaleFactor: window.devicePixelRatio
244
- }
245
- }""" )
246
- print (dimensions)
247
- await browser.close()
248
-
249
- asyncio.run(main())
250
- ```
251
-
252
- </details >
253
-
254
- #### Intercept network requests
255
-
256
- This code snippet sets up request routing for a Chromium page to log all network requests.
257
-
258
- ``` py
259
- from playwright.sync_api import sync_playwright
260
-
261
- with sync_playwright() as p:
262
- browser = p.chromium.launch()
263
- page = browser.new_page()
264
-
265
- def log_and_continue_request (route , request ):
266
- print (request.url)
267
- route.continue_()
268
-
269
- # Log and continue all network requests
270
- page.route(" **/*" , log_and_continue_request)
271
-
272
- page.goto(" http://todomvc.com" )
273
- browser.close()
274
- ```
275
-
276
- <details >
277
- <summary >Async variant</summary >
278
-
279
- ``` py
280
- import asyncio
281
- from playwright.async_api import async_playwright
282
-
283
- async def main ():
284
- async with async_playwright() as p:
285
- browser = await p.chromium.launch()
286
- page = await browser.new_page()
287
-
288
- async def log_and_continue_request (route , request ):
289
- print (request.url)
290
- await route.continue_()
291
-
292
- # Log and continue all network requests
293
- await page.route(" **/*" , log_and_continue_request)
294
- await page.goto(" http://todomvc.com" )
295
- await browser.close()
296
-
297
- asyncio.run(main())
298
- ```
299
-
300
- </details >
301
-
302
- ## Known issues
303
-
304
- ### ` time.sleep() ` leads to outdated state
305
-
306
- You need to use ` page.wait_for_timeout(5000) ` instead of ` time.sleep() ` . It is better to not wait for a timeout at all, but sometimes it is useful for debugging. In these cases, use our wait method instead of the system one. This is because we internally rely on asynchronous operations and when using ` time.sleep(5) ` they can't get processed correctly.
307
-
308
- ## Documentation
48
+ ## Other languages
309
49
310
- Check out our [ new documentation site] ( https://playwright.dev/python/docs/intro ) !
50
+ More comfortable in another programming language? [ Playwright] ( https://playwright.dev ) is also available in
51
+ - [ TypeScript] ( https://playwright.dev/docs/intro )
52
+ - [ .NET] ( https://playwright.dev/dotnet/docs/intro )
53
+ - [ Java] ( https://playwright.dev/java/docs/intro )
0 commit comments