DocumentCompleted没有触发两次

时间:2017-05-24 17:43:46

标签: c# webbrowser-control

抓取网页。该页面加载并调用DocumentCompleted处理程序。在该处理程序中,我调用java方法来设置日期,然后调用单击以获取新数据(通过POST)。这一切都正常工作,只是DocumentCompleted处理程序只被调用一次。返回的POST"得到"新页面不会导致处理程序第二次触发。

我尝试添加多个处理程序,删除第一个处理程序并在第一个处理程序中添加第二个处理程序。没有工作。也以管理员身份运行,没有任何改变。

任何人都有关于如何进行的想法?我想我可以等待60秒加载然后抓住文本,但这看起来很笨拙。

    public void FirstHandler(object sender,
          WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = ((WebBrowser)sender);
        string url = e.Url.ToString();

        if (!(url.StartsWith("http://")) || url.StartsWith("https://"))
        {
            // in AJAX
        }
        if (e.Url.AbsolutePath != webBrowser.Url.AbsolutePath)
        {
            // IFRAME Painting
        }
        else
        {
            // really really complete
            wb.DocumentCompleted -= FirstHandler;
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(SecondHandler);

            HtmlElement webDatePicker = wb.Document.GetElementById("ctl00_WebSplitter1_tmpl1_ContentPlaceHolder1_dtePickerBegin");

            string szJava = string.Empty;
            szJava = "a = $find(\"ctl00_WebSplitter1_tmpl1_ContentPlaceHolder1_dtePickerBegin\"); a.set_text(\"5/20/2017\");";
            object a = wb.Document.InvokeScript("eval", new object[] { szJava });
            if (webDatePicker != null)
                webDatePicker.InvokeMember("submit");

            HtmlElement button = wb.Document.GetElementById("ctl00$WebSplitter1$tmpl1$ContentPlaceHolder1$HeaderBTN1$btnRetrieve");
            if (button != null)
            {
                button.InvokeMember("click");
            }
        }
    }

    public void SecondHandler(object sender, 
        WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser wb = ((WebBrowser)sender);
        string url = e.Url.ToString();
        string d = string.Empty;

        if (!(url.StartsWith("http://")) || url.StartsWith("https://"))
        {
            // in AJAX
        }
        if (e.Url.AbsolutePath != webBrowser.Url.AbsolutePath)
        {
            // IFRAME Painting
        }
        else
        {
                d = wb.DocumentText;
                System.IO.File.WriteAllText("Finally.htm", d);
                wb.DocumentCompleted -= SecondHandler;
        }
        _fired = true;
    }

0 个答案:

没有答案