您的位置:首页 > 其它

webbrowser控件之如何保证网页正常加载

2013-05-27 19:35 441 查看
public void Pause(int seconds)
        {
           // this.Update_Log("Wating " + secondes + " seconds for page to load.");
            DateTime returnTime = DateTime.Now.AddSeconds((double) seconds);
            while(DateTime.Compare(DateTime.Now, returnTime) < 0)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }


Pause()函数可以保证网页在加载完成后,等待多少秒才开始执行Pause()函数后面的代码。

在使用webbrwoser加载网页经常会遇到判断网页什么时候加载成功,或者插入多少等待时间,我在网上看到一个Pause()函数的设计,先分享给大家。

举例说明,我以Facebook自动登录说明Pause()函数的使用方法:

[code]

private void log_in_user(string facebook_user, string facebook_password, int row_count)
        {
            this.tabControl1.SelectedIndex = 1;
            this.Update_Log("Trying to navigate to facebook.com login page");
            this.webBrowser1.Navigate("http://facebook.com/login page.");
            this.Update_Log("Successfully navigate to facebook.com login page");
            this.Update_Log("Checking to see if user account is already logged in.");
            this.Pause();
            try
            {
                HtmlElementCollection links = this.webBrowser1.Document.GetElementsByTagName("input");
                foreach(HtmlElement link in links)
                {
                    if (((link.GetAttribute("value") == "Logout") | (link.GetAttribute("value") == "Log out")) | (link.GetAttribute("value") == "Log Out"))
                    {
                        this.Update_Log("User alrady logged into facebook, logging user out of facebook.");
                        link.InvokeMember("click");
                        this.Update_Log("Sucesfully logged user out of facebook.");
                    }

                    if ((facebook_password == "") | (facebook_user == ""))
                    {
                        this.Update_Log("Enter username and password, or click on a saved user in your user saved list.");
                        this.Update_Log("Waiting for program to start.");
                    }
                    else
                    {
                        try
                        {
                            this.Update_Log("Try to navigate to facebook.com login page");
                            this.Pause();
                            this.webBrowser1.Navigate("http://facebook.com/login.php");
                            this.Update_Log("Sucessfully navigated to facebook.com login page.");
                            this.Pause();
                            this.webBrowser1.Document.GetElementById("email").SetAttribute("value",facebook_user);
                            this.webBrowser1.Document.GetElementById("pass").SetAttribute("value", facebook_password);
                            this.webBrowser1.Document.GetElementById("Login").InvokeMember("Click");
                            this.Pause();
                        }
                        catch (System.Exception exception1)
                        {
                            Exception ex = exception1;
                            this.Update_Log("Error trying to log into facebook.com" + ex.Message.ToString());
                            this.Update_Log("Login stopped.");
                            this.Update_Log("Waiting for program to start.");
                        }
                    }

                }
            }
            catch (System.Exception exception2)
            {
                Exception ex = exception2;
                this.Update_Log("Error trying to log into facebook.com" + ex.Message.ToString());
                this.Update_Log("Login stopped.");
                this.Update_Log("Waiting for program to start.");
            }
        }




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: