I’m trying to catch data frames coming from server via websockets. The data is visible in network tab in devtools.
The bidi and cdp python docs is quite poor, so it’s not clear enough how to catch the income events for the frames.
I tried to use seleniumwire, but it gives only POST/GET packet data.
Here’s the example that I’m testing now. I’m expecting the network_event() to execute when the websocket data comes, but nothing happens. Where am I wrong?
from selenium import webdriver
# network event
def network_event(event):
print(f"Network event {event=}")
options = webdriver.ChromeOptions()
options.add_argument('--blink-settings=imagesEnabled=false')
options.add_argument("--ignore-certificate-errors")
options.add_argument("--ignore-ssl-errors")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--user-data-dir=C:/Users/LEX/AppData/Local/Google/Chrome/User DataDefault")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
try:
driver = webdriver.Chrome(options=options)
except Exception as e:
print(str(e.msg))
try:
dev_tools_, websock_ = driver.start_devtools()
res_ = driver.execute_cdp_cmd('Network.enable', {})
nw = dev_tools_.network
r1 = websock_.on(nw.WebSocketFrameReceived, network_event)
r2 = websock_.on(nw.ResponseReceived, network_event)
driver.get("https://youtube.com")
while driver.title !="":
pass
except Exception as e:
print(str(e))
print('done')
Lexman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.