You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Playwright异步上下文管理器TypeError报错排查与解决咨询

Playwright异步版上下文管理器语法错误解决

问题背景

用Pyrogram抓取Telegram频道消息,提取数据后通过Playwright异步版本自动填充网站表单时,两次因语法错误触发报错:

  • 第一次使用with async_playwright() as playwright:,报错TypeError: 'function' object does not support the context manager protocol
  • 改成async with async_playwright as p:后,又报错TypeError: 'function' object does not support the asynchronous context manager protocol

最初怀疑是Windows同步版切换到Linux异步版导致,后来确认是自身语法疏忽:第一次漏写async关键字,第二次修改时漏写()。


错误代码与对应报错

首次错误代码

@client.on_message(filters.chat(CANAL_ID_IMEX))
async def message_handler(client: Client, message: Message) -> None:
    async for code in get_codes(message):
        with async_playwright() as playwright:
            browser = await playwright.chromium.launch()
            context = await browser.new_context()
            page = await context.new_page()
            await page.goto(URL)

首次报错信息

2024-03-08 21:16:02,915 - INFO - 256 | 2024-03-08 21:16:03 | ZM_Z263A89F4R | 0:00:00.000022 
2024-03-08 21:16:02,915 - ERROR - 'function' object does not support the context manager protocol
Traceback (most recent call last):
  File "/home/svbazuev/Zooma/.venv/lib/python3.11/site-packages/pyrogram/dispatcher.py", line 240, in handler_worker
    await handler.callback(self.client, *args)
  File "/home/svbazuev/Zooma/main.py", line 99, in message_handler
    with async_playwright() as playwright:
TypeError: 'function' object does not support the context manager protocol

二次错误代码

@client.on_message(filters.chat(CANAL_ID_IMEX))
async def message_handler(client: Client, message: Message) -> None:
    async for code in get_codes(message):
        if code:
            async with async_playwright as p:
                browser = await p.chromium.launch()

二次报错信息

2024-03-09 13:02:59,842 - INFO - 274 | 2024-03-09 13:02:59 | ZM_726HBNYRA2 | 0:00:00.000029 
2024-03-09 13:02:59,843 - ERROR - 'function' object does not support the asynchronous context manager protocol
Traceback (most recent call last):
  File "/home/svbazuev/Zooma/.venv/lib/python3.11/site-packages/pyrogram/dispatcher.py", line 240, in handler_worker
    await handler.callback(self.client, *args)
  File "/home/svbazuev/Zooma/main.py", line 100, in message_handler
    async with async_playwright as p:
TypeError: 'function' object does not support the asynchronous context manager protocol

正确用法

Playwright异步版本的上下文管理器必须同时满足两个条件:

  1. 使用async with声明异步上下文
  2. 调用async_playwright()函数(必须带括号,执行后返回支持异步上下文管理的对象)

修正后的核心代码片段:

@client.on_message(filters.chat(CANAL_ID_IMEX))
async def message_handler(client: Client, message: Message) -> None:
    async for code in get_codes(message):
        if code:
            # 正确写法:async with + 带括号的async_playwright()
            async with async_playwright() as p:
                browser = await p.chromium.launch()
                context = await browser.new_context()
                page = await context.new_page()
                await page.goto(URL)
                # 此处添加表单填充、提交等逻辑
                # 操作完成后关闭上下文和浏览器
                await context.close()
                await browser.close()

内容的提问来源于stack exchange,提问作者SVBazuev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 12:50:24