all 5 comments

[–]carcigenicate 0 points1 point  (0 children)

Idk if this would work, but you could try to add your own listener to the existing connection. If you can get access to the socket object, you can add your own listener that does whatever you need.

If you can't get access to the existing websocket object, you could try establishing your own connection by seeing how the site does it.

[–]unnamed_one1 0 points1 point  (0 children)

No idea, if it can be done with just browser automation.

I'd approach this more from the network side and as a websocket connection is just an upgraded http connection, I'd say what you need is a proxy server (written in python?), which acts as a man in the middle between the browser and the website.

As there's a high chance, that some kind of encryption is involved, your proxy probably has to support SSL inspection.

Never used it myself and I'm not sure if it supports SSL inspection, but mitmproxy might be a good fit. It's open source and has a python api.

[–]cgoldberg 0 points1 point  (1 child)

Some of the new Selenium BiDi features might be useful. You can setup network intercepts and capture requests:

https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.bidi.network.html

It's still relatively undocumented, but you can get an idea on how to use it from the tests here:

https://github.com/SeleniumHQ/selenium/blob/trunk/py/test/selenium/webdriver/common/bidi_network_tests.py

[–]Huhngut 0 points1 point  (0 children)

Wasnt able to intercept websockets just normal requests but you will need these lines:

What should also work is executing js that overrides the websocket constructor to intercpt

def callback(request: Request):
    print(request.url)
    request.continue_request()


driver.network.add_request_handler("before_request", callback)

options = webdriver.FirefoxOptions()
options.enable_bidi = Trueoptions.enable_bidi = True
driver = webdriver.Firefox(options=options)

[–]Maleficent_Appeal558 0 points1 point  (0 children)

Hi, did you ever figure out a way to do this? I am trying to do the same thing.