如何解决如何在 C# 中从 WS Chrome 登录读取 Web 套接字消息
我想从 chrome 检查中读取 Web 套接字消息。 我尝试使用 Selenium 但我没有成功。 请帮助我们使用 C# 阅读。
解决方法
请检查以下代码。它捕获并打印客户端/服务器之间发送/接收的消息。但是,这段代码是用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"));
}
});
}
}
,
安装包 AsyncChromeDriver
var asyncChromeDriver = new AsyncChromeDriver();
await asyncChromeDriver.CheckConnected();
var command = new Zu.ChromeDevTools.Network.EnableCommand();
await asyncChromeDriver.DevTools.Network.Enable(command);
var webDriver = new WebDriver(asyncChromeDriver);
await webDriver.Options().Timeouts.SetImplicitWait(TimeSpan.FromSeconds(1));
await webDriver.GoToUrl("WebsiteUrl");
asyncChromeDriver.DevTools.Network.SubscribeToWebSocketFrameReceivedEvent(item =>
{
Log your Data
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。