wkeeling / selenium-wire

Extends Selenium's Python bindings to give you the ability to inspect requests made by the browser.
MIT License
1.9k stars 251 forks source link

Websockets ws_messages is always empty #664

Closed strukiii closed 1 year ago

strukiii commented 1 year ago

Firstly, I appreciate your time put into the project. It's been incredibly useful. I know there is another issue opened on this topic, but I'll add an actual code example that may better explain what I'm doing. I also need to read websocket messages as they come in, but from what I'm doing the print statement print(websocketConnection.ws_messages) is always an empty list? The rest of the requests come through fine, the websocket handshake succeeds and messages are shown on the dev tools console. I know this has been a request before, but I'll say again having an event handler to attach to wss:// requests would be fantastic. Thanks for reading.

keep in mind this is not exactly my code, but it's mostly all there

from seleniumwire.undetected_chromedriver import webdriver as uc

options = uc.ChromeOptions()
driver = uc.Chrome(
        service=Service("chromedriver.exe"),
        options=options,
        advanced_elements=True,
        user_data_dir=accountInfo['chromeProfile'],
        # seleniumwire_options=selOptions,
)
websockets = []

driver.request_interceptor = interceptor
driver.get('https://abc.efg')
sniff()

def interceptor(request):
    now = datetime.datetime.now()
    timestamp = str(now.strftime("%H:%M:%S"))
    print(timestamp, request)
    if (request.url == 'wss://abc.efg'):
        websockets.append(request)
        for message in request.ws_messages:
            print(len(request.ws_messages), message)

def sniff():
    while (1):
        for websocketConnection in websockets:
            print(websocketConnection.ws_messages)
            for message in websocketConnection.ws_messages:
                print(message)