0

我正在尝试从网页中提取数据有很多索引 this.webBrowser1.Document.InvokeScript("__doPostBack", new object[] { "ctl00$ContentPlaceHolder1$gvTickets", "Page$2" }); 这些代码正在工作并转到 webbrowser 中的第二页但是在调试时它不会返回到 documentcompleted 方法。我怎样才能在secodpage上获得responsetext

4

1 回答 1

1

这是一个代码示例。此方法是从控制台应用程序调用的 - 因此如果您使用控制台应用程序,则线程执行并将 [STAThread] 属性添加到您的主方法。documentCompleted 将被递归调用,直到条件失败。

protected override void StartScan( string url )
    {
        WebBrowser browser = new WebBrowser();
        int i = 1;
        Thread thread = new Thread( () =>
        {
            WebBrowserDocumentCompletedEventHandler documentCompleted = null;
            documentCompleted = async ( o, s ) =>
            {

                if ( i < 27 )
                {

                    HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
                    document.LoadHtml(browser.DocumentText);
                    List<Establishment> establishments = GetEstablishments(document).ToList();
                    SaveResults(establishments);
                    string var = i < 10 ? "0" + i.ToString(CultureInfo.InvariantCulture) : i.ToString(CultureInfo.InvariantCulture);
                    browser.Navigate( string.Format( "javascript:__doPostBack('SchoolDirectoryWebPart1$ctl{0}','')", var ) );
                    i++;
                } else {
                    browser.Dispose();
                    Application.ExitThread();
                }
            };


            browser.ScriptErrorsSuppressed = true;
            browser.DocumentCompleted += documentCompleted;
        browser.Navigate( url );
            Application.Run();

        } );
        thread.SetApartmentState( ApartmentState.STA );
        thread.Start();
    }
于 2013-10-25T14:33:25.650 回答