| 4 | |
| 5 | |
| 6 | async def open_browser_and_wait(): |
| 7 | async with async_playwright() as p: |
| 8 | browser = await p.chromium.launch(headless=False) |
| 9 | |
| 10 | context = await browser.new_context( |
| 11 | record_har_path="network_requests.har", # Path to save the HAR file |
| 12 | record_har_content="embed", # Omit content to make the HAR file smaller |
| 13 | # TODO record_har_url_filter="*", # Optional URL filter |
| 14 | ) |
| 15 | |
| 16 | page = await context.new_page() |
| 17 | |
| 18 | print( |
| 19 | "Browser is open. Press Enter in the terminal when you're ready to close the browser and save cookies..." |
| 20 | ) |
| 21 | |
| 22 | input("Press Enter to continue and close the browser...") |
| 23 | |
| 24 | # Ensure 2FA is completed before saving cookies |
| 25 | cookies = await context.cookies() |
| 26 | |
| 27 | with open("cookies.json", "w") as f: |
| 28 | json.dump(cookies, f, indent=4) |
| 29 | |
| 30 | await context.close() |
| 31 | |
| 32 | await browser.close() |
| 33 | |
| 34 | asyncio.run(open_browser_and_wait()) |