we are using selenium on python and as a part of our automation, we need to capture the messages that a sample website sends and receives after the web page loaded completely.
I have check here and it is stated that what we want to do is achievable using BrowserMobProxy
but after testing that, the websocket connection did not work on website and certificate errors were also cumbersome.
In another post here it is stated that, this can be done using loggingPrefs
of Chrome
but it seemed that we only get the logs up to the time when website loads and not the data after that.
Is it possible to capture websocket traffic using only selenium?
Selenium supports Python and thus can be utilized as Selenium WebDriver with Python for testing. Python is easy compared to other programming languages, having far less verbose. The Python APIs empower you to connect with the browser through Selenium.
It extends its support to various browsers like Chrome, Internet Explorer, Safari, Edge, Firefox. To scrape data from these browsers, selenium provides a module called WebDriver, which is useful to perform various tasks like automated testing, getting cookies, getting screenshots, and many more.
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 asyncio
from pyppeteer import launch
async 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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With