Is it possible to capture websocket traffic using selenium and python? Is it possible to capture websocket traffic using selenium and python? selenium selenium

Is it possible to capture websocket traffic using selenium and python?


Turned out that it can be done using pyppeteer; In the following code, all the live websocket traffic of a sample website is being captured:

import asynciofrom pyppeteer import launchasync def main():    browser = await launch(        headless=True,        args=['--no-sandbox'],        autoClose=False    )    page = await browser.newPage()    await page.goto('https://www.tradingview.com/symbols/BTCUSD/')    cdp = await page.target.createCDPSession()    await cdp.send('Network.enable')    await cdp.send('Page.enable')    def printResponse(response):        print(response)    cdp.on('Network.webSocketFrameReceived', printResponse)  # Calls printResponse when a websocket is received    cdp.on('Network.webSocketFrameSent', printResponse)  # Calls printResponse when a websocket is sent    await asyncio.sleep(100)asyncio.get_event_loop().run_until_complete(main())