Playwright可以直接在单文件中执行,使用sync_plywright
就行,但有时我们想让它在框架中运行,再通过API去调用它,这时就会遇到一些问题。
有一些点要注意下:
- 要使用
async_playwright()
而不是sync_playwright()
,而且要在方法前加async
关键字,所有playwright方法执行前加await
关键字,包括对封装了playwright方法的调用。
不然会报如下报错:
1. It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead.
2. PlaywrightContextManager' object does not support the context manager protocol
- uvicorn运行时不要加reload,不然会报
NotImplementedError
错误。