Get Websocket messages with Selenium or any way to capture web socket message with python?

Viewed 1408

Is it possible to get websocket messages with Selenium Webdriver with python? Or any other to way capture or crawl websocket message on specific site?

1 Answers

You can use the 'pyppeteer' library for this

import asyncio
from pyppeteer import launch
 
 
async def main():
    browser = await launch(
        headless=False,
        args=['--no-sandbox'],
        autoClose=False
    )
    page = await browser.newPage()
    await page.goto('https://example.com')
    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(2000)
 
 
asyncio.get_event_loop().run_until_complete(main())
Related