5

I am trying execute a method after the action this.webViewBrowser.CoreWebView2.Navigate(url).

As of yet are failing as the page is not fully load and dynamic scripts executed.

The Microsoft.Web.WebView2 Nuget package is v1.0.781-prerelease.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
nenwmn
  • 89
  • 1
  • 1
  • 5
  • *as the page is not fully load and dynamic scripts executed.* → It basically means you need to wait a bit. There is not such an event to detect when those dynamic content loading scripts are finished. They may work continuously to refresh some information in page. – Reza Aghaei Feb 04 '21 at 21:02

1 Answers1

6

as the page is not fully load and dynamic scripts executed. → It basically means you need to wait a bit. There is not such an event to detect when those dynamic content loading scripts are finished. They may work continuously to refresh some information in page.

As a general answer, for those who are looking for a load event, There are two events which help you detect load event:

  • WebView2.NavigationCompleted → is raised when the WebView has completely loaded (body.onload has been raised) or loading stopped with error.
  • WebView2.CoreWebView2.DOMContentLoaded → is raised when the initial html document has been parsed. This aligns with the the document's DOMContentLoaded event in html. (This one is available starting from 1.0.705.50.)

But just keep in mind, if you have a page which performs some ajax requests (when document is ready), I assume it's obvious that above events will raise before you see all the content. In these cases you just need to wait a bit after the events raised.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • There's also the WebView2.WebMessageReceived event you can use to send a notification from script to native code if like Reza says you need to perform ajax requests or other asynchronous work. There's also the WebView2.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync method which will ensure the provided script is injected into every page to which you navigate just before the page is parsed and any of the page's script runs. – David Risney Feb 04 '21 at 21:39
  • Thank you Reza, yes I am currently using the NavigationCompleted Event and firing too early. Not ideal but will have to use a timer as I am not aware of a sure option. – nenwmn Feb 05 '21 at 21:22
  • 1
    No problem. Do this: `await Task.Delay(3000); DoSomething();`. – Reza Aghaei Feb 06 '21 at 05:57