I'm trying to use Selenium 4 to log requests during manual usage of Chrome browser.
The issue is that request interception stops after around 40 seconds of usage (approximately).
I've tried to change commandTimeout but it didn't change anything. Also I've tried to look into chromedriver logs but I didn't find anithing there.
Here's my code:
static void Main(string[] args)
{
// Enable chromedriver logging
var service = ChromeDriverService.CreateDefaultService();
service.LogPath = AppDomain.CurrentDomain.BaseDirectory + "chromedriver.log";
service.EnableVerboseLogging = true;
var options = new ChromeOptions();
var webDriver = new ChromeDriver(service, options);
var devToolsSession = webDriver.CreateDevToolsSession();
devToolsSession.Network.Enable(new EnableCommandSettings());
EventHandler<RequestInterceptedEventArgs> requestIntercepted = (sender, e) =>
{
Console.WriteLine(e.Request.Url);
};
RequestPattern requestPattern = new RequestPattern();
requestPattern.InterceptionStage = InterceptionStage.Request;
requestPattern.ResourceType = ResourceType.Image;
var setRequestInterceptionCommandSettings = new SetRequestInterceptionCommandSettings();
setRequestInterceptionCommandSettings.Patterns = new RequestPattern[] { requestPattern };
devToolsSession.Network.SetRequestInterception(setRequestInterceptionCommandSettings);
devToolsSession.Network.RequestIntercepted += requestIntercepted;
while (true)
{
webDriver.Url = "https://translate.google.com/";
Thread.Sleep(5000);
webDriver.Navigate().Refresh();
}
}
As of Selenium 4 beta-1, you can use the
FetchAPI and retrieve the same resluts (SetRequestIntercepted is deprecated in ChromeTools as late as December 29, 2020).In either case, the key point for both
RequestIntercepted(prior to deprecation) andFetchto be able to continue after intercepting the request is to capture theRequestId.With
Fetchthis can be done inContinueRequest():Below is your code updated to use
Fetchwhich allowed the query to run for 2+ minutes and 5+ minutes before I manually stopped it:(Note: ensure all of your DevTools as using the same version (i.e. 89 in my case) or you will get errors):
Screenshot (From left-to-right): LinqPad output Console.Write.Url equivalent, Above code in LINQPad, and view of Chrome log file)