@@ -48,97 +48,83 @@ Playwright is built to automate the broad and growing set of web browser capabil
48
48
This code snippet navigates to whatsmyuseragent.org in Chromium, Firefox and WebKit, and saves 3 screenshots.
49
49
50
50
``` 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()
63
60
```
64
61
65
62
#### Mobile and geolocation
66
63
67
64
This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.
68
65
69
66
``` 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()
92
84
```
93
85
94
86
#### Evaluate in browser context
95
87
96
88
This code snippet navigates to example.com in Firefox, and executes a script in the page context.
97
89
98
90
``` 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()
117
106
```
118
107
119
108
#### Intercept network requests
120
109
121
110
This code snippet sets up request routing for a Chromium page to log all network requests.
122
111
123
112
``` py
124
- import asyncio
125
- from playwright import chromium
113
+ from playwright import sync_playwright
126
114
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()
130
118
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_()
134
122
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))
137
125
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()
142
128
```
143
129
144
130
# Is Playwright for Python ready?
0 commit comments