4

I wish to save (secure) WebSocket traffic from some web pages. I tried saving HAR using chrome-devtool. But apparently, it doesn't save the WebSocket traffic. However, I found this answer which suggests I can log all requests if I can change the SDK and log all SDK.networkLog.requests(). Still, to run SDK code, I would need to open the chrome inspector-inspector from Selenium. I know I can open the dev-tool using selenium from this answer. Any suggestion on how to open the inspector-inspector? Or alternate way to log WebSocket? Thanks in Advance!

Hamid
  • 574
  • 3
  • 16
lnman
  • 1,438
  • 4
  • 17
  • 29

2 Answers2

3

You can use wshook to intercept and modify WebSocket requests and message events. Using executeScript() or execute_script() inject the following script to the page.

(function() {
  window.sentMessage = [];
  window.receivedMessage = [];
  var s = document.createElement('script');
  s.src = 'https://cdn.jsdelivr.net/gh/skepticfx/wshook@0.1.1/wsHook.js';
  s.onload = function() {
    window.wsHook.before = function(data, url) {
      window.sentMessage.push(data);
      console.log("Sending message to " + url + " : " + data);
    }

    window.wsHook.after = function(messageEvent, url, wsObject) {
      window.receivedMessage.push(messageEvent.data);
      console.log("Received message from " + url + " : " + messageEvent.data);
      return messageEvent;
    }
  }
  document.body.appendChild(s)
})(); 

After code above executed later you can get the data array from variables window.sentMessage and window.receivedMessage

ewwink
  • 15,852
  • 2
  • 35
  • 50
1

So you can use Chrome Remote Debugging protocol as solution.Starting Chrome 63 even if Selenium is controlling the browser, other clients can connect to chrome and debug it.you can start chrome with the --remote-debugging-port option. I have not personally worked on logging web-socket traffic but have worked on some chrome profiling and this should be able to help. Here are some resources for your review : Chrome dev protocol viewer check if this resource could help chrome-remote-interface also this Selenium + Chrome Dev-tools makes a Perfect Browser Automation Recipe

rs007
  • 365
  • 3
  • 8