我正在使用Asp.Net中的WebBrowser控件。我需要等待DocumentComplete事件中的页面加载。问题在于DocumentComplete事件仅被调用一次。
我在做什么错了?
我的代码:
public partial class HpdScrape : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
getHpdInfo();
}
protected void getHpdInfo()
{
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.AllowNavigation = true;
browser.Navigate("https://hpdonline.hpdnyc.org/HPDonline/provide_address.aspx");
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
if (browser.Url.ToString() == "https://hpdonline.hpdnyc.org/HPDonline/provide_address.aspx")
{
var doc = browser.Document;
doc.GetElementById("ddlBoro").SetAttribute("value", "2");
doc.GetElementById("txtHouseNo").InnerText = "1500";
doc.GetElementById("txtStreet").InnerText = "Washington";
doc.GetElementById("btnSearch").InvokeMember("Click");
// after new page loads this function should get called again...
}
else if (browser.Url.ToString() == "https://hpdonline.hpdnyc.org/hpdonline/select_application.aspx")
{
//code for after 2nd page load.
}
}
}