我遇到了一个例外:导航失败,因为浏览器已断开连接。
在我的代码被执行后,它将被using 块关闭。
浏览器断开连接的主要原因是什么?
这是我得到的例外:
在迭代 0 中查找 Google id 78659 的导航 url、屏幕截图和传出 url 时出错,错误消息是:PuppeteerSharp.NavigationException:导航失败,因为浏览器已断开连接!(NetworkManager 无法处理 Fetch.requestPaused。帧 6CCD5A551750D9DF102ABCF53D90B6EB 未找到。在 PuppeteerSharp.Helpers.AsyncDictionaryHelper`2.<>c__DisplayClass4_0.b__0()
这是我的代码:
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;
}
}
}
}