【问题标题】:webbrowser execution flow浏览器执行流程
【发布时间】:2014-09-28 15:04:16
【问题描述】:

我有这段代码,它只是导航到一个网站

class BrowsingUrl {
    private System.Windows.Forms.WebBrowser nBrowser ;
        private  System.Windows.Forms.Form theFormLayout1;

    public BrowsingUrl(System.Windows.Forms.Form theFormLayout) {

            this.nBrowser = new System.Windows.Forms.WebBrowser();
            this.nBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.nBrowser.Location = new System.Drawing.Point(0, 0);
            this.nBrowser.MinimumSize = new System.Drawing.Size(20, 20);
            this.nBrowser.Name = "webBrowser1";
            this.nBrowser.ScrollBarsEnabled = false;
            this.nBrowser.Size = new System.Drawing.Size(1300, 700);
            this.nBrowser.TabIndex = 0;
            this.theFormLayout1 = theFormLayout;
            this.theFormLayout1.Controls.Add(this.nBrowser);
            this.nBrowser.Navigate("https://stackoverflow.com");
            this.nBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.nBrowser_DocumentCompleted);
   }

   private void nBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
   {

        // do stuff
   }
}    

如果创建一个浏览 URL 对象一切正常,但如果创建两个对象似乎是两个构造函数同时运行,我怎样才能让第二个对象等到第一个对象结束执行

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
        }

        private void startbtn_Click(object sender, EventArgs e)
        {
            BrowsingUrl BrowsingUrl1 = new BrowsingUrl(this);
            //wait till browsingUrl1 finish
            BrowsingUrl BrowsingUrl2 = new BrowsingUrl(this);
        }
}    

【问题讨论】:

标签: c#


【解决方案1】:

在nBrowser_DocumentCompleted 中将_isLoaded 的值设置为true,并使其作为公共属性可访问。同时添加ScriptErrorsSuppressed = true。

    class BrowsingUrl
    {
        private System.Windows.Forms.WebBrowser nBrowser;
        private System.Windows.Forms.Form theFormLayout1;
        private bool _isLoaded = false;
        public bool IsLoaded
        {
            get { return _isLoaded && !nBrowser.IsBusy && 
                  nBrowser.ReadyState == WebBrowserReadyState.Complete; }
        }
        public BrowsingUrl(System.Windows.Forms.Form theFormLayout)
        {

            this.nBrowser = new System.Windows.Forms.WebBrowser();
            this.nBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.nBrowser.Location = new System.Drawing.Point(0, 0);
            this.nBrowser.MinimumSize = new System.Drawing.Size(20, 20);
            this.nBrowser.Name = "webBrowser1";
            this.nBrowser.ScrollBarsEnabled = false;
            this.nBrowser.Size = new System.Drawing.Size(1300, 700);
            this.nBrowser.TabIndex = 0;
            this.theFormLayout1 = theFormLayout;
            this.theFormLayout1.Controls.Add(this.nBrowser);
            this.nBrowser.ScriptErrorsSuppressed = true;
            this.nBrowser.Navigate("https://stackoverflow.com");
            this.nBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.nBrowser_DocumentCompleted);
        }
        private void nBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //check for frames
            if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
                return;

            // do stuff
            //Sample do stuff
            Thread.Sleep(10000);
            // end do stuff

            _isLoaded = true;
        }
    }  

然后你可以等到BrowsingUrl1被加载:

        BrowsingUrl BrowsingUrl1 = new BrowsingUrl(this);
        //Wait for the first page to load
        while (!BrowsingUrl1.IsLoaded)
        {
            Application.DoEvents();
        }
        BrowsingUrl BrowsingUrl2 = new BrowsingUrl(this);

【讨论】:

  • 谢谢您的回复,我尝试了这个解决方案,但没有运气两个实例都在运行,但是这个解决方案让我清楚地知道两个实例在两个单独的线程中运行,
  • 是的,我现在看到了,它不适用于某些页面。问题是页面内有 javascript 异步执行。我会尝试寻找其他方法。
  • @magicalll 更新了答案,经过测试,似乎工作正常。但也许您的问题可能是您想要访问已加载文档(do stuff)中的某些 HTML,并且您想要访问修改后的 HTML。那将是一个不同的问题。
  • 是的,你是绝对正确的,如果我尝试在 nBrowser_DocumentCompleted 中“做一些事情”,就会出现你提到的问题,否则解决方案是完美的,但如果我可以问你最后一个问题,什么方法你会建议操纵 dom
  • @magicalll 我使用 Selenium Web 驱动程序docs.seleniumhq.org/projects/webdriver,或者你可以使用watin.org。这些是用于对网页进行单元测试的工具(甚至包含 AJAX 等)
猜你喜欢
  • 2019-06-17
  • 2013-04-08
  • 2010-12-23
  • 2016-06-24
  • 2019-10-31
  • 2016-10-07
  • 2011-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多