Skip to content

Commit 02b6ea0

Browse files
authored
docs: leave async examples in README.md (microsoft#108)
1 parent beb0f22 commit 02b6ea0

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

README.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ with sync_playwright() as p:
7070
iphone_11 = p.devices['iPhone 11 Pro']
7171
browser = p.webkit.launch(headless=False)
7272
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']
7777
)
7878
page = context.newPage()
7979
page.goto('https://maps.google.com')
@@ -83,6 +83,32 @@ with sync_playwright() as p:
8383
browser.close()
8484
```
8585

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+
86112
#### Evaluate in browser context
87113

88114
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:
105131
browser.close()
106132
```
107133

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+
108158
#### Intercept network requests
109159

110160
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:
127177
browser.close()
128178
```
129179

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+
130204
# Is Playwright for Python ready?
131205

132206
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

Comments
 (0)