我使用 WebBrowser-Control 模拟登录,然后从 HTNML 源的页面中读取数据。
如果我用 2 个按钮和 2 次单击来完成它,它会完美运行:
private void btnTest_Click(object sender, EventArgs e)
{
Login();
}
private void btnTest2_Click(object sender, EventArgs e)
{
ReadData();
}
private static void Login()
{
browser.Navigate("www.site.de");
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
richLog.Text += "Warten...";
Thread.Sleep(1000);
}
browser.Document.GetElementById("login_username").SetAttribute("value", username);
browser.Document.GetElementById("login_password").SetAttribute("value", password);
browser.Document.GetElementById("login_submit_6b86b171").InvokeMember("click");
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
richLog.Text += Environment.NewLine + "Warten...";
Thread.Sleep(10000);
}
}
private static void ReadData()
{
browser.Navigate("www.site.de/data");
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
richLog.Text += Environment.NewLine + "Warten...";
Thread.Sleep(1000);
}
richText.Text = browser.DocumentText;
richLog.Text += Environment.NewLine + "Posts ausgelesen - Seite " + site;
}
如果我用一个按钮来做,它会失败。打开的站点是处于未登录状态的站点。我尝试添加 10 秒或更长时间的 Thread.Sleep(),但响应是相同的。
private void btnTest_Click(object sender, EventArgs e)
{
Login();
ReadData();
}
有什么不同?怎么解决?