【bug】with sync_playwright as p: AttributeError: enter
环境
playwright 1.46.0
详情
在Python中使用Playwright时,遇到了AttributeError: __enter__错误。错误原因是使用with语句来管理一个不支持上下文管理协议的对象。
经过检查,发现是写法错误,失误将sync_playwright()
写出了sync_playwright
。😄
解决方法
下面是sync_playwright()
正确的写法例子
from playwright.sync_api import sync_playwrightwith sync_playwright() as p:browser = p.chromium.launch()# 你的操作...browser.close()