C# WebBrowser control — Get Document Elements After AJAX?

后端 未结 7 787
[愿得一人]
[愿得一人] 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:35

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

提交回复
热议问题