fysh711426 / UndetectedChromeDriver

GNU General Public License v3.0
179 stars 66 forks source link

Get the body of the response to the request #39

Open maestroant opened 1 year ago

maestroant commented 1 year ago

The task is to send a request and use DevTools to intercept the response. I try like this:

            var options = new ChromeOptions();
            string selfDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            var driverExecutablePath = selfDir + @"\chromedriver.exe";
            options.AddArgument("--disable-features=IsolateOrigins,site-per-process");

            var driver = UndetectedChromeDriver.Create(options: options, driverExecutablePath: driverExecutablePath,
                browserExecutablePath: _browserExecutablePath);

            // log request url
            var devTools = ((IDevTools)driver).GetDevToolsSession()
                .GetVersionSpecificDomains<DevToolsVer.DevToolsSessionDomains>();
            await devTools.Network.Enable(new DevToolsVer.Network.EnableCommandSettings());
            devTools.Network.RequestWillBeSent += (sender, e) =>
            {

                var responseBody = devTools.Network.GetResponseBody(new GetResponseBodyCommandSettings { RequestId = e.LoaderId }).GetAwaiter().GetResult();
                Console.WriteLine(responseBody.Body);
                //Console.WriteLine(e.Request.Url);
            };

            driver.Navigate().GoToUrl("https://prepaid.t-mobile.com/bring-your-own-device");

exception: System.InvalidOperationException: "A command response was not received: Network.getResponseBody"

Does anyone know how to get the body of the page???

fysh711426 commented 1 year ago

Try it. ResponseReceived

devTools.Network.ResponseReceived += async (sender, e) =>
{
    try
    {
        var responseBody = await devTools.Network.GetResponseBody(
            new DevToolsVer.Network.GetResponseBodyCommandSettings { RequestId = e.RequestId });
        Console.WriteLine(responseBody.Body);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
};
maestroant commented 1 year ago

Thank you! You are a great magician! :)