我为我解决了这个问题.
关键是,为onPropertyChanged
通过ajax调用填充的div元素的事件附加一个处理程序.
HtmlElement target = webBrowser.Document.GetElementById("div_populated_by_ajax"); if (target != null) { target.AttachEventHandler("onpropertychange", handler); }
最后,
private void handler(Object sender, EventArgs e) { HtmlElement div = webBrowser.Document.GetElementById("div_populated_by_ajax"); if (div == null) return; String contentLoaded = div.InnerHtml; // get the content loaded via ajax }
using System; using System.Windows.Forms; namespace WebBrowserDemo { class Program { public const string TestUrl = "http://www.w3schools.com/Ajax/tryit_view.asp?filename=tryajax_first"; [STAThread] static void Main(string[] args) { WebBrowser wb = new WebBrowser(); wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); wb.Navigate(TestUrl); while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(true); } static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser wb = (WebBrowser)sender; HtmlElement document = wb.Document.GetElementsByTagName("html")[0]; HtmlElement button = wb.Document.GetElementsByTagName("button")[0]; Console.WriteLine(document.OuterHtml + "\n"); button.InvokeMember("Click"); Console.WriteLine(document.OuterHtml); } } }