@@ -48,97 +48,83 @@ Playwright is built to automate the broad and growing set of web browser capabil
4848This code snippet navigates to whatsmyuseragent.org in Chromium, Firefox and WebKit, and saves 3 screenshots.
4949
5050``` py
51- import asyncio
52- from playwright import chromium, firefox, webkit
53-
54- async def run ():
55- for browser_type in [chromium, firefox, webkit]:
56- browser = await browser_type.launch()
57- page = await browser.newPage()
58- await page.goto(' http://whatsmyuseragent.org/' )
59- await page.screenshot(path = f ' example- { browser_type.name} .png ' )
60- await browser.close()
61-
62- asyncio.get_event_loop().run_until_complete(run())
51+ from playwright import sync_playwright
52+
53+ with sync_playwright() as p:
54+ for browser_type in [p.chromium, p.firefox, p.webkit]:
55+ browser = browser_type.launch()
56+ page = browser.newPage()
57+ page.goto(' http://whatsmyuseragent.org/' )
58+ page.screenshot(path = f ' example- { browser_type.name} .png ' )
59+ browser.close()
6360```
6461
6562#### Mobile and geolocation
6663
6764This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.
6865
6966``` py
70- import asyncio
71- from playwright import webkit, devices
72-
73- iphone_11 = devices[' iPhone 11 Pro' ]
74- print (iphone_11)
75-
76- async def run ():
77- browser = await webkit.launch(headless = False )
78- context = await browser.newContext(
79- ** iphone_11,
80- locale = ' en-US' ,
81- geolocation = { ' longitude' : 12.492507 , ' latitude' : 41.889938 },
82- permissions = [' geolocation' ]
83- )
84- page = await context.newPage()
85- await page.goto(' https://maps.google.com' )
86- await page.click(' text="Your location"' )
87- await page.waitForRequest(' *preview/pwa' )
88- await page.screenshot(path = ' colosseum-iphone.png' )
89- await browser.close()
90-
91- asyncio.get_event_loop().run_until_complete(run())
67+ from playwright import sync_playwright
68+
69+ with sync_playwright() as p:
70+ iphone_11 = p.devices[' iPhone 11 Pro' ]
71+ browser = p.webkit.launch(headless = False )
72+ context = browser.newContext(
73+ ** iphone_11,
74+ locale = ' en-US' ,
75+ geolocation = { ' longitude' : 12.492507 , ' latitude' : 41.889938 },
76+ permissions = [' geolocation' ]
77+ )
78+ page = context.newPage()
79+ page.goto(' https://maps.google.com' )
80+ page.click(' text="Your location"' )
81+ page.waitForRequest(' *preview/pwa' )
82+ page.screenshot(path = ' colosseum-iphone.png' )
83+ browser.close()
9284```
9385
9486#### Evaluate in browser context
9587
9688This code snippet navigates to example.com in Firefox, and executes a script in the page context.
9789
9890``` py
99- import asyncio
100- from playwright import firefox
101-
102- async def run ():
103- browser = await firefox.launch()
104- page = await browser.newPage()
105- await page.goto(' https://www.example.com/' )
106- dimensions = await page.evaluate(''' () => {
107- return {
108- width: document.documentElement.clientWidth,
109- height: document.documentElement.clientHeight,
110- deviceScaleFactor: window.devicePixelRatio
111- }
112- }''' )
113- print (dimensions)
114- await browser.close()
115-
116- asyncio.get_event_loop().run_until_complete(run())
91+ from playwright import sync_playwright
92+
93+ with sync_playwright() as p:
94+ browser = p.firefox.launch()
95+ page = browser.newPage()
96+ page.goto(' https://www.example.com/' )
97+ dimensions = page.evaluate(''' () => {
98+ return {
99+ width: document.documentElement.clientWidth,
100+ height: document.documentElement.clientHeight,
101+ deviceScaleFactor: window.devicePixelRatio
102+ }
103+ }''' )
104+ print (dimensions)
105+ browser.close()
117106```
118107
119108#### Intercept network requests
120109
121110This code snippet sets up request routing for a Chromium page to log all network requests.
122111
123112``` py
124- import asyncio
125- from playwright import chromium
113+ from playwright import sync_playwright
126114
127- async def run () :
128- browser = await chromium.launch()
129- page = await browser.newPage()
115+ with sync_playwright() as p :
116+ browser = p. chromium.launch()
117+ page = browser.newPage()
130118
131- def log_and_continue_request (route , request ):
132- print (request.url)
133- await asyncio.create_task( route.continue_() )
119+ def log_and_continue_request (route , request ):
120+ print (request.url)
121+ route.continue_()
134122
135- # Log and continue all network requests
136- await page.route(' **' , lambda route , request : log_and_continue_request(route, request))
123+ # Log and continue all network requests
124+ page.route(' **' , lambda route , request : log_and_continue_request(route, request))
137125
138- await page.goto(' http://todomvc.com' )
139- await browser.close()
140-
141- asyncio.get_event_loop().run_until_complete(run())
126+ page.goto(' http://todomvc.com' )
127+ browser.close()
142128```
143129
144130# Is Playwright for Python ready?
0 commit comments