question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WaitForNavigationAsync should resolve if Frame already has requested lifecycle

See original GitHub issue

I expect that calling:

await page.WaitForNavigationAsync();

would resolve immediately if the page have already collected ‘load’ lifecycle event before. Also, I expect the following sequence should complete successfully instead of hanging forever as it does currently with version 5.1.0 of puppeteer-sharp:

await page.GoToAsync(url, WaitUntilNavigation.DOMContentLoaded);
await page.WaitForNavigationAsync();

As a workaround for my project I slightly modified FrameManager.cs lines 149-164 to be:

        public async Task<Response> WaitForFrameNavigationAsync(Frame frame, NavigationOptions options = null)
        {
            var timeout = options?.Timeout ?? TimeoutSettings.NavigationTimeout;
            using (var watcher = new LifecycleWatcher(this, frame, options?.WaitUntil, timeout))
            {
                var raceTask = await Task.WhenAny(
                    watcher.NewDocumentNavigationTask,
                    watcher.SameDocumentNavigationTask,
                    watcher.LifecycleTask, // <-- add this line to resolve lifecycle events without waiting for actual navigation
                    watcher.TimeoutOrTerminationTask).ConfigureAwait(false);

                await raceTask.ConfigureAwait(false);

                return watcher.NavigationResponse;
            }
        }

Please check if it may be acceptable fix for public, or maybe a separate method for waiting for lifecycle events without doing actual navigation should be added.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kblokcommented, Oct 8, 2021

The most common pattern is doing something like this:

await Task.WhenAll(
    Page.ClickAsync("a"),
    Page.WaitForNavigationAsync());

I think that with the change you’re suggesting, WaitForNavigationAsync will immediately be resolved, breaking that pattern.

0reactions
igristovcommented, Oct 12, 2021

High chances you can reproduce the issue with this simple test:

class Program
{
    static async Task Main(string[] args)
    {
        await new BrowserFetcher().DownloadAsync();
        await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });
        var sites = new[] { "news.yahoo.com", "news.google.com", "cnn.com", "wsj.com", "news.bbc.co.uk", "latimes.com" };
        Task.WaitAll(sites.Select(x => Test(browser, "https://" + x)).ToArray());
    }
    static async Task Test(Browser browser, string url)
    {
        await using var page = await browser.NewPageAsync();
        await page.SetRequestInterceptionAsync(true);
        page.Request += async (s, e) => await e.Request.ContinueAsync();
        page.DOMContentLoaded += (s, e) => Debug.WriteLine($"DOMContentLoaded {page.Url} [{url}]");
        page.Load += (s, e) => Debug.WriteLine($"Load {page.Url} [{url}]");
        await page.GoToAsync(url);
    }
}

You’ll catch all DOMContentloaded but only few Load events…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use PuppeteerSharp.Frame.NavigatedWithinDocument in ...
Use the NavigatedWithinDocument method in PuppeteerSharp package in your next Puppeteer-sharp project with LambdaTest Automation Testing Advisor.
Read more >
Frame | Playwright .NET
Frame object's lifecycle is controlled by three events, dispatched on the page ... If current document has already reached the required state, resolves...
Read more >
Untitled
WaitForNavigationAsync should resolve if Frame already has requested lifecycle #1841. evaluate resolves to undefined. Until(driver => driver.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found