hardkoded / puppeteer-sharp

Headless Chrome .NET API
https://www.puppeteersharp.com
MIT License
3.34k stars 439 forks source link

Random NavigationException exceptions on page.GoToAsync() #1447

Closed mr-wong closed 4 years ago

mr-wong commented 4 years ago

The code is fairly simple:

    private void button1_Click(object sender,EventArgs e) {
        PuppeteerSharp.LaunchOptions opts = new PuppeteerSharp.LaunchOptions() {
            Headless=false,
            ExecutablePath=@"C:\Users\user\AppData\Local\Chromium\Application\chrome.exe",
            DefaultViewport=null,
            UserDataDir=@"d:\32323"
        };

        opts.IgnoredDefaultArgs=new string[] { "--enable-automation" };

        PuppeteerSharp.Launcher launcher = new PuppeteerSharp.Launcher();

        var browser=launcher.LaunchAsync(opts).GetAwaiter().GetResult();

        var pages=browser.PagesAsync().GetAwaiter().GetResult();

        var page = pages.ElementAt(0);

        for(int i = 0;i<20;i++) {
            Navigate(page,"https://distilnetworks.com");
            Navigate(page,"https://mail.com");
            Navigate(page,"https://distilnetworks.com");
            Navigate(page,"https://vk.com");
            Navigate(page,"https://distilnetworks.com");
            Navigate(page,"https://mail.com");
            Navigate(page,"https://distilnetworks.com");
            Navigate(page,"https://mail.com");
            Navigate(page,"about:blank");
        }
    }

    public void Navigate(PuppeteerSharp.Page page,string url) {
        page.MainFrame.GoToAsync(url,new PuppeteerSharp.NavigationOptions() { Timeout=0,WaitUntil=new PuppeteerSharp.WaitUntilNavigation[] { PuppeteerSharp.WaitUntilNavigation.DOMContentLoaded } }).GetAwaiter().GetResult();
    }

I always get random exceptions mostly like this one:

PuppeteerSharp.NavigationException: 'Protocol error(Page.navigate): Target closed. (NetworkManager failed to process Network.requestWillBeSent. Frame 53F547F3C375005301129799DB86C5A4 not found. at PuppeteerSharp.Helpers.AsyncDictionaryHelper2.<>c__DisplayClass4_0.<GetItemAsync>b__0() in D:\Downloads\puppeteer-sharp-master\lib\PuppeteerSharp\Helpers\AsyncDictionaryHelper.cs:line 32 at PuppeteerSharp.Helpers.TaskHelper.<WithTimeout>d__61.MoveNext() in D:\Downloads\puppeteer-sharp-master\lib\PuppeteerSharp\Helpers\TaskHelper.cs:line 109 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at PuppeteerSharp.Helpers.AsyncDictionaryHelper2.d4.MoveNext() in D:\Downloads\puppeteer-sharp-master\lib\PuppeteerSharp\Helpers\AsyncDictionaryHelper.cs:line 31 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at PuppeteerSharp.NetworkManager.d46.MoveNext() in D:\Downloads\puppeteer-sharp-master\lib\PuppeteerSharp\NetworkManager.cs:line 309 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at PuppeteerSharp.NetworkManager.d__49.MoveNext() in D:\Downloads\puppeteer-sharp-master\lib\PuppeteerSharp\NetworkManager.cs:line 385 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at PuppeteerSharp.NetworkManager.d__40.MoveNext() in D:\Downloads\puppeteer-sharp-master\lib\PuppeteerSharp\NetworkManager.cs:line 141)'

Or even like this:

System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'

This exception was originally thrown at this call stack: System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource) System.Collections.Generic.List.Enumerator.MoveNextRare() System.Collections.Generic.List.Enumerator.MoveNext() PuppeteerSharp.LifecycleWatcher.CheckLifecycle(PuppeteerSharp.Frame, System.Collections.Generic.IEnumerable) in LifecycleWatcher.cs PuppeteerSharp.LifecycleWatcher.CheckLifecycleComplete() in LifecycleWatcher.cs PuppeteerSharp.LifecycleWatcher.LifecycleWatcher(PuppeteerSharp.FrameManager, PuppeteerSharp.Frame, PuppeteerSharp.WaitUntilNavigation[], int) in LifecycleWatcher.cs PuppeteerSharp.FrameManager.NavigateFrameAsync(PuppeteerSharp.Frame, string, PuppeteerSharp.NavigationOptions) in FrameManager.cs System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) System.Runtime.CompilerServices.TaskAwaiter.GetResult() ... [Call Stack Truncated]

Chromium version: Version 82.0.4076.0 (Official Build) (32-bit)

How can I solve this issue? :( Thanks.

kblok commented 4 years ago

Aren't you awaiting the tasks returned by Navigate?

mr-wong commented 4 years ago

Aren't you awaiting the tasks returned by Navigate?

Yes I wait using page.GoToAsync(...).GetAwaiter().GetResult(); so all the pages navigated one by one. It can happen at the very first GoToAsync call or after hundred calls. I tried both the latest Chromium version and the version offered by the downloader with the same result. Also tried to downgrade Puppeteer with no luck.

ben-biddington commented 4 years ago

Failing example there in #1450, but I can't progress further than that.

Reduced the iteration size a bit to speed it up.

Have not seen the second-mentioned error case yet, only the first.