Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

selenium - How to Read Web Socket Messages from WS Chrome log in C#

I Want to read Web Socket messages from chrome inspect. I try to use Selenium but I could not success. please help us to read using C#.

enter image description here

question from:https://stackoverflow.com/questions/65640739/how-to-read-web-socket-messages-from-ws-chrome-log-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Please check below code. It captures and print the messages sent/received between client/server. However, this code is in Java

import org.json.JSONObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.logging.Level;

public class WebSocketLogging {
    
    private static WebDriver driver;
    
    public static void main(String[] args) throws InterruptedException {
        LoggingPreferences loggingprefs = new LoggingPreferences();
        loggingprefs.enable(LogType.PERFORMANCE, Level.ALL);

        DesiredCapabilities cap = new DesiredCapabilities().chrome();
        cap.setCapability(CapabilityType.LOGGING_PREFS, loggingprefs);
        
        System.setProperty("webdriver.chrome.driver", "C:\Sarabjeet\Box\chromedriver.exe");

        driver = new ChromeDriver(cap);
        driver.navigate().to("https://web-demo.adaptivecluster.com/");
        Thread.sleep(5000);
        LogEntries logEntries = driver.manage().logs().get(LogType.PERFORMANCE);

        driver.close();
        driver.quit();
        logEntries.forEach(entry->{
            JSONObject messageJSON = new JSONObject(entry.getMessage());
            String method = messageJSON.getJSONObject("message").getString("method");
            if(method.equalsIgnoreCase("Network.webSocketFrameSent")){
                System.out.println("Message Sent: " + messageJSON.getJSONObject("message").getJSONObject("params").getJSONObject("response").getString("payloadData"));
            }else if(method.equalsIgnoreCase("Network.webSocketFrameReceived")){
                System.out.println("Message Received: " + messageJSON.getJSONObject("message").getJSONObject("params").getJSONObject("response").getString("payloadData"));
            }
        });
    }

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...