C#WebBrowser控件 - 在AJAX之后获取文档元素?

aik*_*eru 20 javascript c# browser ajax

我正在编写一个使用WebBrowser控件来查看可以使用AJAX更改新内容/元素的Web内容的应用程序.我似乎无法以任何方式尝试新的元素.BrowserCtl.DocumentText没有最新页面,当然它也不在"查看源代码"中.

有没有办法使用此控件获取此新数据?:(请帮忙.谢谢!

IE:

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 :(
Run Code Online (Sandbox Code Playgroud)

小智 19

我为我解决了这个问题.

关键是,为onPropertyChanged通过ajax调用填充的div元素的事件附加一个处理程序.

HtmlElement target = webBrowser.Document.GetElementById("div_populated_by_ajax");

if (target != null)
{
      target.AttachEventHandler("onpropertychange", handler);
}
Run Code Online (Sandbox Code Playgroud)

最后,

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
}
Run Code Online (Sandbox Code Playgroud)


xia*_*ian 5

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);           
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这继续呈现旧的HTML.试过.Net 4.0 (2认同)

Chl*_*hil -1

你控制网页吗?

如果是这样,这里有一篇博客文章解释了如何操作: http://www.palladiumconsulting.com/blog/sebastian/2007/04/ultimate-intranet-toy.html

如果没有,可能有解决方案,但我无法帮助您,抱歉。