C# WebBrowser 控件——在 AJAX 之后获取文档元素? [英] C# WebBrowser control -- Get Document Elements After AJAX?
问题描述
我正在编写一个应用程序,该应用程序使用 WebBrowser 控件来查看 Web 内容,这些内容可以通过添加新内容/元素的 AJAX 进行更改.我似乎无法以任何尝试过的方式获得新元素.BrowserCtl.DocumentText 没有最新的页面,当然它也不在查看源代码"中.
I'm writing an application that uses the WebBrowser control to view web content that can change with AJAX that adds new content/elements. I can't seem to get at the new elements any way I've tried. BrowserCtl.DocumentText doesn't have the up-to-date page and of course it's not in "view source" either.
有什么方法可以使用此控件获取这些新数据?:(请帮忙.谢谢!
Is there some way to get this new data using this control? :( Please help. Thanks!
浏览器:
Browser.Navigate("www.somewebpagewithAJAX.com");
//Code that waits for browser to finish...
...
//WebBrowser control has loaded content and AJAX has loaded new content
// (is visible at runtime on form) but can't see them in Browser.Document.All
// or Browser.DocumentText :(
推荐答案
我为我解决了这个问题.
I solved the problem for me.
关键是,为通过 ajax 调用填充的 div 元素的 onPropertyChanged
事件附加一个处理程序.
the key is, attaching a handler for onPropertyChanged
event of the div element which is being populated via ajax call.
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
}
这篇关于C# WebBrowser 控件——在 AJAX 之后获取文档元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!