我有一系列我想要运行的代码。
问题是在运行#3之后,#4代码在等待登录页面加载之前开始运行。
我尝试使用WebBrowserDocumentCompletedEventHandler
,但每次都显示错误。
以下是我的代码:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ClearCookies);
webBrowser.Navigate("https://www.example.com/");
}
void ClearCookies(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Login1);
webBrowser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
}
void LoginPage(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Login1);
webBrowser.Navigate("https://example.com/Login/");
}
void Login(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Login2);
webBrowser.Document.GetElementById("Email").InnerText = "email@gmail.com";
HtmlElementCollection elc = this.webBrowser.Document.GetElementsByTagName("input");
foreach (HtmlElement el in elc)
{
if (el.GetAttribute("type").Equals("submit"))
{
el.InvokeMember("Click");
}
}
}
void form(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Nothing);
webBrowser.Document.GetElementById("name").InnerText = "test";
HtmlElementCollection elc2 = this.webBrowser.Document.GetElementsByTagName("input");
foreach (HtmlElement el in elc2)
{
if (el.GetAttribute("type").Equals("submit"))
{
el.InvokeMember("Click");
}
}
}
void Nothing(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
我还得到另一个错误:
this.mainWbrowser.DocumentTitle错误CS1061:'Form1'不包含'mainWbrowser'的定义,并且没有可以找到接受类型'Form1'的第一个参数的扩展方法'mainWbrowser'(你是否缺少using指令或汇编参考?)