0

I'm getting an exception: Navigation failed because browser has disconnected.

After my code gets executed, it will close by the using block.

What is the main reason for the browser to disconnect?.

Here's the exception that I get:

Error on finding navigate urls, screenshot and outgoing urls for Google id 78659 for location in in iteration 0 and error message is : PuppeteerSharp.NavigationException: Navigation failed because browser has disconnected! (NetworkManager failed to process Fetch.requestPaused. Frame 6CCD5A551750D9DF102ABCF53D90B6EB not found. at PuppeteerSharp.Helpers.AsyncDictionaryHelper`2.<>c__DisplayClass4_0.b__0()

This is my code:

using (var destinationUrlBrowser = await Puppeteer.LaunchAsync(launchOptions))
    {
        using (var page = await destinationUrlBrowser.NewPageAsync())
        {
            try
            {
                var viewPortOptions = new ViewPortOptions{Width = 1366, Height = 768};
                await page.SetViewportAsync(viewPortOptions);
                await page.AuthenticateAsync(new Credentials{Username = _luminatiUserName, Password = _luminatiPassword});
                var timeout = new NavigationOptions{Timeout = 50000};
                Response response;
                try
                {
                    response = await page.GoToAsync(destinationUrl, 1 * 50000);
                }
                catch (Exception)
                {
                    response = await page.ReloadAsync(timeout);
                }

                await Task.Delay(2000);
                var pageSource = await page.GetContentAsync();
                if (string.IsNullOrEmpty(pageSource) || pageSource.Contains("Proxy Error"))
                {
                    isDestinationPageSourceNotFound = true;
                }

                if (!isDestinationPageSourceNotFound)
                {
                    var screenShotOptions = new ScreenshotOptions{FullPage = true, Type = ScreenshotType.Jpeg, Quality = 50};
                    Log.Info($"Taking screen shot for {network} id {Id} for location {country}!");
                    await page.ScreenshotAsync(screenShotUrl, screenShotOptions);
                    var chain = response.Request.RedirectChain;
                    var redirects = new HashSet<string>();
                    for (var index = 0; index < chain.Length - 1; index++)
                    {
                        redirects.Add(chain[index].Url);
                    }

                    destinationInfo.Redirects = redirects.ToList();
                    var currentUrl = response.Request.Url;
                    for (int i = 0; i < 2; i++)
                    {
                        response = await page.GoToAsync(currentUrl);
                        var currentUrl1 = response.Request.Url;
                        if (currentUrl != currentUrl1)
                        {
                            currentUrl = currentUrl1;
                            continue;
                        }
                        else
                        {
                            currentUrl = currentUrl1;
                            break;
                        }
                    }

                    destinationInfo.DestinationUrl = currentUrl;
                    Log.Info($"Found {destinationInfo.Redirects.Count} redirect urls from {network} id {Id} for location {country}!");
                    destinationInfo.HtmlPageSource = pageSource;
                    using (var outputFile = new StreamWriter(indexHtml))
                    {
                        await outputFile.WriteAsync(pageSource);
                    }

                    destinationInfo.HtmlContent = Helper.RemoveStyleAndScript(destinationInfo.HtmlPageSource);
                    using (var outputFile = new StreamWriter(htmlContentUrl))
                    {
                        await outputFile.WriteAsync(destinationInfo.HtmlContent);
                    }

                    ZipFile.CreateFromDirectory(basePath, zipPath);
                    destinationInfo.LocalHtmlPath = zipPath;
                    destinationInfo.LocalScreenShot = screenShotUrl;
                    outgoingUrls = Helper.GetOutgoingUrl(pageSource, currentUrl);
                    Log.Info($"Found {outgoingUrls.Count} outgoing urls from {network} id {Id} for location {country}!");
                    foundDetails = true;
                }
            }
            catch (Exception e)
            {
                Log.Info($"Error on finding navigate urls, screenshot and outgoing urls for {network} id {Id} for location {country} in iteration {iterate} and error message is : {e}");
                if (iterate > 1)
                {
                    isDestinationPageSourceNotFound = true;
                }
            }
        }
    }
Abzoozy
  • 874
  • 12
  • 26
Siddu
  • 1
  • 2
  • One thing I see: If your authentication fails, you catch an exception, ignore it, and just keep going like nothing is wrong. You might want to reconsider that logic. – tgolisch Dec 19 '19 at 14:25

1 Answers1

4

If your code used to work, and it stopped working all of a sudden.

You won't believe it! Chrome version matters so basically if you are using Version 83.0.4103.61 (Official Build) (64-bit), it will fail and show "Navigation failed because browser has disconnected!" Error.

downgrade your chrome to Version 81.0.4044.138 (Official Build) (64-bit) or below 7X.XX.XXXX.XXX

Note: Make sure you do the same in your server as well. Not just locally

Abzoozy
  • 874
  • 12
  • 26