我正在尝试调试当我尝试启动浏览器会话时
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
从调试日志来看,
nodriver
无法初始化浏览器连接。我怀疑这可能是 nodriver
和 Chromium 之间的版本兼容性问题,或者是 nodriver
如何在无头模式下处理 WebSocket 通信的问题。
有人在最新版本的 Python、
nodriver
或 Chromium 中遇到过类似的问题吗?是否有解决方法或配置调整可以解决浏览器启动过程中的NoneType
错误?
谢谢您提供的信息。