0

I am using the WebBrowser Component in System.Windows.Forms. The code loads content from a website and returns it properly. There is a JavaScript which is executed and loading some of the DOMs after the page has loaded completely.

The JavaScript is not finished loading by the time the .Navigate method finished execution. If I set a Breakpoint on the .Navigate in Debug mode, It will, clearly because .Navigate is asynchronous, run through the process of loading the page including the scripts.

void LoadPageWithScripts() {
  Browser.Navigate("mypagewithscriptsurl");
  // whatever comes next prevents the DOM generated by the script from beeing loaded

  // ... e.g.:
  Console.WriteLine("whatever");

  // use Browser.Document later
}

I know, this question is similar to the one provided here: JavaScript only works...

Unfortunately, I have no Influence on the page which is loaded, so the approaches I have seen there, are not suitable for my needs.

I have tried to simply work with Thread.Sleep, as suggested by many forums. But even this won't work. As soon as the code continues to run past the .Navigate method, the JavaScript is lost. Only setting a break point on it will work currently.

Browser.Navigate("pageUrl");

Browser.Navigate("pageurl");

// Very bad solution
Thread.Sleep(2000);

while (true)
{
    if (Browser.ReadyState == WebBrowserReadyState.Complete)
    {
        // do something
        break;
    }
    else
    {
        Application.DoEvents();
    }
}

Using the DocumentCompleted Event will not work, since the Script is not loaded before the document is in completed state.

Browser.Navigate("pageUrl");

Browser.DocumentCompleted += (o, e) =>
{
    var text = Browser.DocumentText;
    Console.WriteLine(text);
};

Hope to find some help.

Michael Staples
  • 537
  • 7
  • 13
  • The webbrowser `DocumentCompleted` event fires once the document has finished loading. Why not handle that and do whatever you need to do at that point? – Handbag Crab Dec 05 '18 at 14:33
  • I tried exactly to use the DocumentCompleted Event, but as with reaching the loop, or any next step of the code, the script generated content will not be loaded with it. – Michael Staples Dec 05 '18 at 14:38
  • Share the URL or an HTML content to reproduce the problem. ([MCVE]) – Reza Aghaei Dec 05 '18 at 14:41
  • 1
    The `DocumentCompleted` event can be raised multiple times. You need to use this event to verify whether `Browser.ReadyState == WebBrowserReadyState.Complete`. When it is, check the Document content. This verification might be performed more than once. Setup your code to handle it. It has always been like this. – Jimi Dec 05 '18 at 20:51
  • Thank you @Jimi for the hint of the event being raised multiple times. Indeed this helps in a combination of checking the DOM, which unfortunately has some performance issues, but the approach at least works. – Michael Staples Dec 06 '18 at 09:22
  • After several approaches, I decided to use cefsharp, which does the job like a champ. https://cefsharp.github.io/ – Michael Staples Dec 06 '18 at 20:40

0 Answers0