5

我正在使用Awesomium 1.7.0.5来加载页面、填充一些文本框并单击按钮。我正在尝试使用此线程中的示例填充文本框:http ://answers.awesomium.com/questions/857/webcontrol-type-in​​-webbrowser.html

这是我的代码(我正在使用 WPF 控件):

        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"; 
                }

            }
        }

它有效,但仅适用于 Thread.Sleep 0.1-0.5 秒。否则文档为空(非空)和/或文本框为空。我该怎么办?为什么它在 DocumentReadyEvent 中没有准备好?

4

2 回答 2

5

这是我解决它的方法:

     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 而不是 DocumentReady,因为它不仅在我需要它时触发,而且在应用程序启动时触发,我在导航之前订阅它并在它之后取消订阅。还要检查它是否为 IsMainFrame。

编辑:但使用此解决方案有时会引发文档未准备好的异常。所以我也在等待它使用 Thread.Sleep。

于 2013-04-18T16:43:47.120 回答
0

很老的问题。但对于像我这样面临这个问题的人来说——使用LoadingFrameCompleted事件WebControl.IsNavigating == falsee.IsMainFrame == true条件。如果这些条件在此事件中为真,则页面已完成加载,您已准备好获取 HTML 内容。

于 2018-05-05T11:04:45.293 回答