TypeError:在 Python 3.12 上使用 nodriver 启动浏览器时无法解压“NoneType”对象

问题描述 投票:0回答:1

我正在尝试调试当我尝试启动浏览器会话时

nodriver
发生的错误。该代码曾经可以工作,但现在在更新到新设置后会引发错误。这是重现该问题的简化脚本:

import nodriver as uc
import time
import logging

logging.basicConfig(level=logging.DEBUG)

async def test_nodriver():
    agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
    try:
        browser = await uc.start(headless=True, browser_args=[f"--user-agent={agent}"])
        logging.info("Browser started successfully")
        page = await browser.get("https://www.nowsecure.nl")

        time.sleep(1)

        await browser.start.save_screenshot("image.png")
        logging.info("Screenshot saved successfully")
    except Exception as e:
        logging.error(f"Failed to start browser: {e}")

if __name__ == "__main__":
    uc.loop().run_until_complete(test_nodriver())

错误回溯

运行此脚本后,我收到以下错误回溯:

Traceback (most recent call last):
  File "/home/me/project/src/test_nodriver.py", line 22, in <module>
    uc.loop().run_until_complete(test_nodriver())
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/me/project/src/test_nodriver.py", line 10, in test_nodriver
    browser = await uc.start(headless=True, browser_args=[f"--user-agent={agent}"])
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/me/project/.venv/lib/python3.12/site-packages/nodriver/core/util.py", line 96, in start
    return await Browser.create(config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/me/project/.venv/lib/python3.12/site-packages/nodriver/core/browser.py", line 91, in create
    await instance.start()
  File "/home/me/project/.venv/lib/python3.12/site-packages/nodriver/core/browser.py", line 394, in start
    await self.connection.send(cdp.target.set_discover_targets(discover=True))
  File "/home/me/project/.venv/lib/python3.12/site-packages/nodriver/core/connection.py", line 420, in send
    await self._prepare_headless()
  File "/home/me/project/.venv/lib/python3.12/site-packages/nodriver/core/connection.py", line 499, in _prepare_headless
    response, error = await self._send_oneshot(
    ^^^^^^^^^^^^^^^
TypeError: cannot unpack non-iterable NoneType object

环境信息

  • 操作系统:Linux Ubuntu 24.04.1 LTS
  • Python: 3.12.6
  • 无驱动程序: 0.37
  • Chromium: 130.0.6723.69(通过 Snap 安装)

问题描述

从调试日志来看,

nodriver
无法初始化浏览器连接。我怀疑这可能是
nodriver
和 Chromium 之间的版本兼容性问题,或者是
nodriver
如何在无头模式下处理 WebSocket 通信的问题。

有人在最新版本的 Python、

nodriver
或 Chromium 中遇到过类似的问题吗?是否有解决方法或配置调整可以解决浏览器启动过程中的
NoneType
错误?

python-3.x ubuntu web-scraping python-asyncio chromium
1个回答
0
投票

谢谢您提供的信息。

© www.soinside.com 2019 - 2024. All rights reserved.