From d1782d1c876630ecd25fd76f421f5c1186e359f5 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 8 Feb 2024 07:21:23 -0800 Subject: [PATCH] aider: Remove asynchronous code and use synchronous Playwright API. # Aider chat conversation: USER: don't use async ASSISTANT: Here is the updated `playw.py` file without using async: ```diff --- playw.py +++ playw.py @@ ... @@ -import asyncio -from playwright.async_api import async_playwright +from playwright.sync_api import sync_playwright import sys -async def main(url): - async with async_playwright() as p: - browser = await p.chromium.launch() - page = await browser.new_page() - await page.goto(url) - content = await page.content() - print(content) - await browser.close() +def main(url): + with sync_playwright() as p: + browser = p.chromium.launch() + page = browser.new_page() + page.goto(url) + content = page.content() + print(content) + browser.close() if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python playw.py ") sys.exit(1) - asyncio.run(main(sys.argv[1])) + main(sys.argv[1]) ``` This diff removes the asynchronous code and replaces it with the synchronous version provided by Playwright's `sync_api`. The `async` and `await` keywords are removed, and the `async_playwright` context manager is replaced with `sync_playwright`. The script should now run synchronously. --- playw.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/playw.py b/playw.py index 484f6b599..4266983a1 100644 --- a/playw.py +++ b/playw.py @@ -1,18 +1,17 @@ -import asyncio -from playwright.async_api import async_playwright +from playwright.sync_api import sync_playwright import sys -async def main(url): - async with async_playwright() as p: - browser = await p.chromium.launch() - page = await browser.new_page() - await page.goto(url) - content = await page.content() +def main(url): + with sync_playwright() as p: + browser = p.chromium.launch() + page = browser.new_page() + page.goto(url) + content = page.content() print(content) - await browser.close() + browser.close() if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python playw.py ") sys.exit(1) - asyncio.run(main(sys.argv[1])) + main(sys.argv[1])