python playwright 无法使用同步模式
我想用playwright同步模式模拟浏览器,但报错 需要使用异步模式的API求大佬帮忙
使用异步API的方法:
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.goto('https://example.com')
# Perform other async operations with the page here
await browser.close()
if __name__ == '__main__':
asyncio.run(main()) import playwright.sync_api
with playwright.sync_api.sync_playwright() as pw:
browser = pw.firefox.launch(headless=False)
page = browser.new_page()
page.goto("https://www.baidu.com")
browser.close()
我的电脑上这样是没问题的 建议用codegen 试一下,重新生成一下脚本
playwright codegen -o test.py --channel msedge
页:
[1]