@@ -70,10 +70,10 @@ with sync_playwright() as p:
70
70
iphone_11 = p.devices[' iPhone 11 Pro' ]
71
71
browser = p.webkit.launch(headless = False )
72
72
context = browser.newContext(
73
- ** iphone_11,
74
- locale = ' en-US' ,
75
- geolocation = { ' longitude' : 12.492507 , ' latitude' : 41.889938 },
76
- permissions = [' geolocation' ]
73
+ ** iphone_11,
74
+ locale = ' en-US' ,
75
+ geolocation = { ' longitude' : 12.492507 , ' latitude' : 41.889938 },
76
+ permissions = [' geolocation' ]
77
77
)
78
78
page = context.newPage()
79
79
page.goto(' https://maps.google.com' )
@@ -83,6 +83,32 @@ with sync_playwright() as p:
83
83
browser.close()
84
84
```
85
85
86
+ ... or, if you are comfortable using asyncio, you can do the following:
87
+
88
+ ``` py
89
+ import asyncio
90
+ from playwright import async_playwright
91
+
92
+ async def main ():
93
+ async with async_playwright() as p:
94
+ iphone_11 = p.devices[' iPhone 11 Pro' ]
95
+ browser = await p.webkit.launch(headless = False )
96
+ context = await browser.newContext(
97
+ ** iphone_11,
98
+ locale = ' en-US' ,
99
+ geolocation = { ' longitude' : 12.492507 , ' latitude' : 41.889938 },
100
+ permissions = [' geolocation' ]
101
+ )
102
+ page = await context.newPage()
103
+ await page.goto(' https://maps.google.com' )
104
+ await page.click(' text="Your location"' )
105
+ await page.waitForRequest(' *preview/pwa' )
106
+ await page.screenshot(path = ' colosseum-iphone.png' )
107
+ await browser.close()
108
+
109
+ asyncio.get_event_loop().run_until_complete(main())
110
+ ```
111
+
86
112
#### Evaluate in browser context
87
113
88
114
This code snippet navigates to example.com in Firefox, and executes a script in the page context.
@@ -105,6 +131,30 @@ with sync_playwright() as p:
105
131
browser.close()
106
132
```
107
133
134
+ ... and again, async version:
135
+
136
+ ``` py
137
+ import asyncio
138
+ from playwright import async_playwright
139
+
140
+ async def main ():
141
+ async with async_playwright() as p:
142
+ browser = await p.firefox.launch()
143
+ page = await browser.newPage()
144
+ await page.goto(' https://www.example.com/' )
145
+ dimensions = await page.evaluate(''' () => {
146
+ return {
147
+ width: document.documentElement.clientWidth,
148
+ height: document.documentElement.clientHeight,
149
+ deviceScaleFactor: window.devicePixelRatio
150
+ }
151
+ }''' )
152
+ print (dimensions)
153
+ await browser.close()
154
+
155
+ asyncio.get_event_loop().run_until_complete(main())
156
+ ```
157
+
108
158
#### Intercept network requests
109
159
110
160
This code snippet sets up request routing for a Chromium page to log all network requests.
@@ -127,6 +177,30 @@ with sync_playwright() as p:
127
177
browser.close()
128
178
```
129
179
180
+ ... async version:
181
+
182
+ ``` py
183
+ import asyncio
184
+ from playwright import async_playwright
185
+
186
+ async def main ():
187
+ async with async_playwright() as p:
188
+ browser = await p.chromium.launch()
189
+ page = await browser.newPage()
190
+
191
+ def log_and_continue_request (route , request ):
192
+ print (request.url)
193
+ asyncio.create_task(route.continue_())
194
+
195
+ # Log and continue all network requests
196
+ await page.route(' **' , lambda route , request : log_and_continue_request(route, request))
197
+
198
+ await page.goto(' http://todomvc.com' )
199
+ await browser.close()
200
+
201
+ asyncio.get_event_loop().run_until_complete(main())
202
+ ```
203
+
130
204
# Is Playwright for Python ready?
131
205
132
206
We are ready for your feedback, but we are still covering Playwright Python with the tests, so expect a bumpy ride and don't use for production.
0 commit comments