|
16 | 16 |
|
17 | 17 | from playwright import browser_types
|
18 | 18 |
|
| 19 | + |
19 | 20 | async def run():
|
20 |
| - print('Launching browser...') |
21 |
| - browser = await browser_types['webkit'].launch(headless=False) |
22 |
| - print('Contexts in browser: %d' % len(browser.contexts)) |
23 |
| - print('Creating context...') |
| 21 | + print("Launching browser...") |
| 22 | + browser = await browser_types["webkit"].launch(headless=False) |
| 23 | + print("Contexts in browser: %d" % len(browser.contexts)) |
| 24 | + print("Creating context...") |
24 | 25 | context = await browser.newContext(viewport=None)
|
25 |
| - print('Contexts in browser: %d' % len(browser.contexts)) |
26 |
| - print('Pages in context: %d' % len(context.pages)) |
| 26 | + print("Contexts in browser: %d" % len(browser.contexts)) |
| 27 | + print("Pages in context: %d" % len(context.pages)) |
27 | 28 |
|
28 |
| - print('\nCreating page1...') |
| 29 | + print("\nCreating page1...") |
29 | 30 | page1 = await context.newPage()
|
30 |
| - print('Pages in context: %d' % len(context.pages)) |
31 |
| - page1.on('framenavigated', lambda frame: print('Frame navigated to %s' % frame.url)) |
32 |
| - page1.on('request', lambda request: print('Request %s' % request.url)) |
33 |
| - page1.on('requestFinished', lambda request: print('Request finished %s' % request.url)) |
34 |
| - page1.on('response', lambda response: print('Response %s, request %s in frame %s' % (response.url, response.request.url, response.frame.url))) |
35 |
| - print('Navigating page1 to https://example.com...') |
36 |
| - await page1.goto('https://example.com') |
37 |
| - print('Page1 main frame url: %s' % page1.mainFrame.url) |
38 |
| - print('Page1 tile: %s' % await page1.title()) |
39 |
| - print('Frames in page1: %d' % len(page1.frames)) |
40 |
| - await page1.screenshot(path='example.png') |
| 31 | + print("Pages in context: %d" % len(context.pages)) |
| 32 | + page1.on("framenavigated", lambda frame: print("Frame navigated to %s" % frame.url)) |
| 33 | + page1.on("request", lambda request: print("Request %s" % request.url)) |
| 34 | + page1.on( |
| 35 | + "requestFinished", lambda request: print("Request finished %s" % request.url) |
| 36 | + ) |
| 37 | + page1.on( |
| 38 | + "response", |
| 39 | + lambda response: print( |
| 40 | + "Response %s, request %s in frame %s" |
| 41 | + % (response.url, response.request.url, response.frame.url) |
| 42 | + ), |
| 43 | + ) |
| 44 | + print("Navigating page1 to https://example.com...") |
| 45 | + await page1.goto("https://example.com") |
| 46 | + print("Page1 main frame url: %s" % page1.mainFrame.url) |
| 47 | + print("Page1 tile: %s" % await page1.title()) |
| 48 | + print("Frames in page1: %d" % len(page1.frames)) |
| 49 | + await page1.screenshot(path="example.png") |
41 | 50 |
|
42 |
| - print('\nCreating page2...') |
| 51 | + print("\nCreating page2...") |
43 | 52 | page2 = await context.newPage()
|
44 |
| - page2.on('framenavigated', lambda frame: print('Frame navigated to %s' % frame.url)) |
| 53 | + page2.on("framenavigated", lambda frame: print("Frame navigated to %s" % frame.url)) |
45 | 54 |
|
46 |
| - print('Navigating page2 to https://webkit.org...') |
47 |
| - await page2.goto('https://webkit.org') |
48 |
| - print('Page2 tile: %s' % await page2.title()) |
49 |
| - print('Pages in context: %d' % len(context.pages)) |
| 55 | + print("Navigating page2 to https://webkit.org...") |
| 56 | + await page2.goto("https://webkit.org") |
| 57 | + print("Page2 tile: %s" % await page2.title()) |
| 58 | + print("Pages in context: %d" % len(context.pages)) |
50 | 59 |
|
51 |
| - print('\nQuerying body...') |
52 |
| - body1 = await page1.querySelector('body') |
53 |
| - print('Body text %s' % await body1.textContent()) |
| 60 | + print("\nQuerying body...") |
| 61 | + body1 = await page1.querySelector("body") |
| 62 | + print("Body text %s" % await body1.textContent()) |
54 | 63 |
|
55 |
| - print('Closing page1...') |
| 64 | + print("Closing page1...") |
56 | 65 | await page1.close()
|
57 |
| - print('Pages in context: %d' % len(context.pages)) |
| 66 | + print("Pages in context: %d" % len(context.pages)) |
58 | 67 |
|
59 |
| - print('Navigating page2 to https://cnn.com...') |
60 |
| - await page2.goto('https://cnn.com') |
61 |
| - print('Page2 main frame url: %s' % page2.mainFrame.url) |
62 |
| - print('Page2 tile: %s' % await page2.title()) |
63 |
| - print('Frames in page2: %d' % len(page2.frames)) |
64 |
| - print('Pages in context: %d' % len(context.pages)) |
| 68 | + print("Navigating page2 to https://cnn.com...") |
| 69 | + await page2.goto("https://cnn.com") |
| 70 | + print("Page2 main frame url: %s" % page2.mainFrame.url) |
| 71 | + print("Page2 tile: %s" % await page2.title()) |
| 72 | + print("Frames in page2: %d" % len(page2.frames)) |
| 73 | + print("Pages in context: %d" % len(context.pages)) |
65 | 74 |
|
66 |
| - print('Closing context...') |
| 75 | + print("Closing context...") |
67 | 76 | await context.close()
|
68 |
| - print('Contexts in browser: %d' % len(browser.contexts)) |
69 |
| - print('Closing browser') |
| 77 | + print("Contexts in browser: %d" % len(browser.contexts)) |
| 78 | + print("Closing browser") |
70 | 79 | await browser.close()
|
71 | 80 |
|
| 81 | + |
72 | 82 | asyncio.get_event_loop().run_until_complete(run())
|
0 commit comments