我正试图在我的webBrowser应用程序中使用C#控件自动登录到我的网站。网页检查填写和提交登录表格所需的时间。如果花费的时间少于2秒,那么它会显示一个“错误页面”,上面写着“机器人不允许”。
现在,我的应用程序也得到了“错误页面”,仅仅是因为登录表单在2秒内就被填满了。如何在启动‘InvokeMember(“单击”)之前添加延迟,使网页计算出的填写表单的时间超过2秒。
这是我的密码
HtmlElement ele = WebBrowser1.Document.GetElementById("username");
if (ele != null)
{
ele.InnerText = "myUsrname";
}
ele = webBrowser1.Document.GetElementById("password");
if (ele != null)
{
ele.InnerText = "myPasswrd";
}
ele = webBrowser1.Document.GetElementById("submit");
if (ele != null)
{
// I want to add a delay of 3 seconds here
ele.InvokeMember("click");
}注意:我使用了“Task.Delay(3000)”,但它似乎不起作用。
编辑:这是我现在使用的,正在为我工作。
async void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
{
......//my code
await Task.Delay(3000);// this is where I wanted to put a delay
....
}但我想,这是正确的使用方法吗?
发布于 2015-09-24 11:27:03
如果您不想在等待时冻结UI,请考虑以下示例:
private async void button1_Click(object sender, EventArgs e)
{
await Task.Run(async () =>
{
await Task.Delay(3000);
MessageBox.Show("1");
button1.Invoke(new Action(() => { this.button1.Text = "1"; }));
});
MessageBox.Show("2");
button1.Invoke(new Action(() => { this.button1.Text = "2"; }));
}样品是自描述的。
发布于 2015-09-24 11:17:30
你可以用这个:
int milliseconds = 2000;
Thread.Sleep(milliseconds) 大家好,
ST
https://stackoverflow.com/questions/32759789
复制相似问题