0

我遇到了一个例外:导航失败,因为浏览器已断开连接。

在我的代码被执行后,它将被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;
                }
            }
        }
    }
4

1 回答 1

4

如果您的代码过去可以工作,但它突然停止工作。

你不会相信的!Chrome 版本很重要,如果您使用的是版本 83.0.4103.61(官方构建)(64 位),它将失败并显示“导航失败,因为浏览器已断开连接!” 错误。

将您的 chrome 降级到版本 81.0.4044.138 (Official Build) (64-bit) 或低于 7X.XX.XXXX.XXX

注意:确保您在服务器中也执行相同的操作。不仅仅是本地

于 2020-06-02T07:49:24.050 回答