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;
}
}
}
}