很奇怪的问题。
我必须要刷新3次页面(不是使用refresh,而是axWebBrowser1.Navigate(ub.Uri.ToString(), ref flags, ref targetframe, ref post, ref headers);)才会正确填写用户名及密码。第一次在位置1出错,第二次在位置2中出错,第三次在位置3中出错,但是即使不在try中也都不报错,就是直接跳到username.setAttribute("value","test",0);请大侠指点。代码如下:private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
UriBuilder ub = new UriBuilder("http://mail.ABCD.cn/cgi-bin/index.cgi?"); this.labelWebPageStatus.Text = "页面打开完成,地址:" + ub.Uri.ToString(); HTMLDocument doc = (HTMLDocument)(this.axWebBrowser1.Document); HTMLLIElement username = null;
HTMLLIElement password = null;
HTMLLIElement mail_domain;
HTMLLIElement login2;

try
{
username = (HTMLLIElement)(doc.getElementById("username"));//位置1

password = (HTMLLIElement)(doc.getElementById("password"));//位置2
mail_domain = (HTMLLIElement)(doc.getElementById("mail_domain"));
login2 = (HTMLLIElement)(doc.getElementById("login2"));//位置3
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

username.setAttribute("value","test",0); password.setAttribute("value","XXX",0);
mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)axWebBrowser1.Document;
mshtml.IHTMLFormElement f1 =(mshtml.IHTMLFormElement)doc2.all.item ("login2",0);

((mshtml.IHTMLFormElement)(f1)).submit();

}