How Can Read Web Page Using WebBrowser control

后端 未结 1 837
陌清茗
陌清茗 2020-12-04 00:40
string urlt = webBrowser1.Url.ToString();
Webbrowser1.Navigate(\"Google.com\")

        HtmlElement elem;
        if (webBrowser1.Document != null)
        {
                


        
相关标签:
1条回答
  • 2020-12-04 01:26

    From the comment:

    .. but async or await is not supported i think iam using vs2010 and i already installed Nuget but still iam finding async keyword, please help

    If you can't use async/await, then you can't use for loop for asynchronous WebBrowser navigation, unless resorting to deprecated hacks with DoEvents. Use the state pattern, that's what C# 5.0 compiler generates behind the scene for async/await.

    Alternatively, if you're adventurous enough, you can simulate async/await with yield, as described here.

    Updated, below is another way of exploiting the C# enumerator state machine (compatible with C# 2.0 and later):

    using System;
    using System.Collections;
    using System.Windows.Forms;
    
    namespace WindowsForms_22296644
    {
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
            }
    
            IEnumerable GetNavigator(string[] urls, MethodInvoker next)
            {
                WebBrowserDocumentCompletedEventHandler handler =
                    delegate { next(); };
    
                this.webBrowser.DocumentCompleted += handler;
                try
                {
                    foreach (var url in urls)
                    {
                        this.webBrowser.Navigate(url);
                        yield return Type.Missing;
                        MessageBox.Show(this.webBrowser.Document.Body.OuterHtml);
                    }
                }
                finally
                {
                    this.webBrowser.DocumentCompleted -= handler;
                }
            }
    
            void StartNavigation(string[] urls)
            {
                IEnumerator enumerator = null;
                MethodInvoker next = delegate { enumerator.MoveNext(); };
                enumerator = GetNavigator(urls, next).GetEnumerator();
                next();
            }
    
            private void Form_Load(object sender, EventArgs e)
            {
                StartNavigation(new[] { 
                    "http://example.com",
                    "http://example.net",
                    "http://example.org" });
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题