C# WebBrowser control — Get Document Elements After AJAX?

后端 未结 7 789
[愿得一人]
[愿得一人] 2020-11-27 14:01

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 el

7条回答
  •  佛祖请我去吃肉
    2020-11-27 14:28

    I solved the problem for me.

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

    and finally,

    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
    }
    

提交回复
热议问题