Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document is not ready in DocumentReady event?

Tags:

c#

.net

awesomium

I am using Awesomium 1.7.0.5 in order to load a page, fill some textboxes and click a button. I am trying to fill a textbox using an example from this thread: http://answers.awesomium.com/questions/857/webcontrol-type-in-webbrowser.html

Here is my code (I am using WPF control):

        private void WbAwsOnDocumentReady(object sender, UrlEventArgs urlEventArgs)
        {
            if (wbAws == null || !wbAws.IsLive)
              return;

            //Thread.Sleep(555);

            dynamic document = (JSObject)wbAws.ExecuteJavascriptWithResult("document");

            if (document == null)
              return;

            using (document)
            {
                dynamic textbox = document.getElementById("email");

                if (textbox == null)
                  return;

                using (textbox)
                {
                    textbox.value = "gaaffa"; 
                }

            }
        }

It works but only with Thread.Sleep for 0.1-0.5 sec. Otherwise document is empty (not null) and/or textbox is null. What should I do? Why it is not ready in DocumentReadyEvent?

like image 730
Alex P. Avatar asked Apr 18 '13 09:04

Alex P.


1 Answers

Here is how I solved it:

     WbAws.LoadingFrameCompleted += OnLoadingFrameCompleted;
     WbAws.Source = new Uri("http://google.com");

private void OnLoadingFrameCompleted(...)
{ 
   if (webView == null || !webView.IsLive || 
         webView.ParentView != null || !e.IsMainFrame)
     return;

    LoadingFrameCompleted -= OnLoadingFrameCompleted;

    // do something
}

LoadingFrameCompleted instead of DocumentReady and because it fires not only when I need it but also on app startup I subscribe to it just before navigating and unsubscribe after it. Also checking that it IsMainFrame.

edit: but with this solution it sometimes throws exception that document is not ready. So I am also waiting for it using Thread.Sleep.

like image 66
Alex P. Avatar answered Sep 30 '22 07:09

Alex P.