Python: 在 pyppeteer 中保持打开浏览器并创建 CDPSession

Python: keep open browser in pyppeteer and create CDPSession

我有两个问题暂时无法解决。

1. 我想保留浏览器 运行 这样我就可以使用 pyppeteer.launcher.connect() function but it seems to be closed imidiately even if I don't call pyppeteer.browser.Browser.close().

重新连接

test01.py:

import asyncio

from pyppeteer import launch, connect

async def fetch():
    browser = await launch(
        headless=False,
        args=['--no-sandbox']
    )
    print(f'Endpoint: {browser.wsEndpoint}')
    await browser.disconnect()

loop = asyncio.get_event_loop()
loop.run_until_complete(fetch())

$ python test01.py
Endpoint: ws://127.0.0.1:51757/devtools/browser/00e917a9-c031-499a-a8ee-ca4090ebd3fe
$ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" http://127.0.0.1:51757
curl: (7) Failed to connect to 127.0.0.1 port 51757: Connection refused

2. 如何创建 CDP 会话。这段代码应该会打开另一个浏览器 window 但它没有按预期工作:

test02.py

import asyncio
import time

from pyppeteer import launch, connect

async def fetch():
    browser = await launch(
        headless=False,
        args=['--no-sandbox']
    )
    page = await browser.newPage()
    cdp = await page.target.createCDPSession()
    await cdp.send('Target.createBrowserContext')
    time.sleep(5)
    await browser.disconnect()

loop = asyncio.get_event_loop()
loop.run_until_complete(fetch())

$ python test02.py
Future exception was never retrieved
future: <Future finished exception=NetworkError('Protocol error Target.sendMessageToTarget: Target closed.',)>
pyppeteer.errors.NetworkError: Protocol error Target.sendMessageToTarget: Target closed.

如何保持浏览器运行

你只需要使用 autoClose 标志,这里是 docs:

autoClose (bool): Automatically close browser process when script completed. Defaults to True.

在这种情况下,您 test01.py 将如下所示:

import asyncio

from pyppeteer import launch, connect

async def fetch():
    browser = await launch(
        headless=False,
        args=['--no-sandbox'],
        autoClose=False
    )
    print(f'Endpoint: {browser.wsEndpoint}')
    await browser.disconnect()

loop = asyncio.get_event_loop()
loop.run_until_complete(fetch())

CDP 会话

这里是:

import asyncio
import time

from pprint import pprint

from pyppeteer import launch, connect
from pyppeteer.browser import BrowserContext

async def fetch():
    browser = await launch(
        headless=False,
        args=['--no-sandbox'],
        autoClose=False
    )
    page = await browser.newPage()
    cdp = await page.target.createCDPSession()
    raw_context = await cdp.send('Target.createBrowserContext')
    pprint(raw_context)
    context = BrowserContext(browser, raw_context['browserContextId'])
    new_page = await context.newPage()
    await cdp.detach()
    await browser.disconnect()

loop = asyncio.get_event_loop()
loop.run_until_complete(fetch())

灵感来自 Browser.createIncognitoBrowserContext 来自 pyppeteer 本身。 请注意,通过 CDP 创建额外的会话似乎不是一个好主意,因为 browser._contexts 不会更新并且会变得不一致。 Browser.createIncognitoBrowserContext 也有可能在不求助于 CDP 的情况下满足您的需求