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
});
var driver = new ChromeDriver(options);
IDevTools devTools = driver as IDevTools;
var session = devTools.GetDevToolsSession();
// May need to use a different version of DevTools besides V119
var networkAdapter = session.GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V119.DevToolsSessionDomains>().Network;
networkAdapter.WebSocketCreated += NetworkAdapter_WebSocketCreated;
networkAdapter.WebSocketFrameReceived += NetworkAdapter_WebSocketFrameReceived;
var websocketCommandSettings = new OpenQA.Selenium.DevTools.V119.Network.EnableCommandSettings();
networkAdapter.Enable(websocketCommandSettings);
字符串
查找连接
private void NetworkAdapter_WebSocketCreated(object sender, OpenQA.Selenium.DevTools.V119.Network.WebSocketCreatedEventArgs e)
{
if (!e.Url.Contains("wss://whatever.url.you.want.to.filter.on"))
{
Debug.WriteLine($"Ignoring the following Request URL({e.Url}), Request Id{e.RequestId}");
return;
}
StoredRequestId = e.RequestId;
Debug.WriteLine($"YAY!!! Received the Websocket connection of interest, Request URL({e.Url}), Request Id{e.RequestId}");
}
型
使用框架
private void NetworkAdapter_WebSocketFrameReceived(object sender, OpenQA.Selenium.DevTools.V119.Network.WebSocketFrameReceivedEventArgs e)
{
// Note:the StoredRequestId would need to be a class private variable/property
if (e.RequestId != StoredRequestId)
{
return;
}
Debug.WriteLine($"Received Websocket Frame for requestId({e.RequestId}), contains: {e.Response.PayloadData}");
}
3条答案
按热度按时间uqxowvwt1#
请检查下面的代码。它捕获并打印客户端/服务器之间发送/接收的消息。然而,此代码是Java代码
字符串
bis0qfac2#
安装包AsyncChromeDriver
字符串
zysjyyx43#
下面是如何使用Selenium读取websockets框架,你可以做以下事情。
设置监听器逻辑
字符串
查找连接
型
使用框架
型
注意事项:这个解决方案可能可以改进,以过滤请求,就像你可以用Fetch API过滤一样。但是我还没有研究过,也没有找到一个例子。