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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With