Oneday0 发表于 2023-3-25 13:55

python playwright 无法使用同步模式

我想用playwright同步模式模拟浏览器,但报错 需要使用异步模式的API
求大佬帮忙



Klock0828 发表于 2023-3-25 15:01

使用异步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())

hiplease 发表于 2023-3-25 16:48

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()


我的电脑上这样是没问题的

louis16s 发表于 2023-3-25 21:12

建议用codegen 试一下,重新生成一下脚本
playwright codegen -o test.py --channel msedge
页: [1]
查看完整版本: python playwright 无法使用同步模式