我定义了一个类,里面有一个方法fabu,用于主窗体调用,主窗体里有 webbrowser,主窗体里这样写:Classfabu classfb = new Classfabu();
classfb.fabu(webbrowser1); // 把webbrowser1 当做参数传递到类类的代码如下:class Classfabu
{ public void fabu(object obj)
{
WebBrowser wb = (obj as WebBrowser);
wb.Navigate("http://login.sina.com.cn/signup/signin.php"); Thread.Sleep(5000);
//登陆填表
HtmlElement tbUserid = wb.Document.All["username"];
HtmlElement tbPasswd = wb.Document.All["password"];
tbUserid.SetAttribute("value", uname);
tbPasswd.SetAttribute("value", upass);
foreach (HtmlElement element in wb.Document.All)
if (element.TabIndex == 5) element.InvokeMember("click");
}
}
传递参数是正常的,就是遇到 操作 wb.Document 的时候 根本没有办法操作下去,也就无法完成自动填表,
请教各位这是为什么,有什么解决的方法么?
classfb.fabu(webbrowser1); // 把webbrowser1 当做参数传递到类类的代码如下:class Classfabu
{ public void fabu(object obj)
{
WebBrowser wb = (obj as WebBrowser);
wb.Navigate("http://login.sina.com.cn/signup/signin.php"); Thread.Sleep(5000);
//登陆填表
HtmlElement tbUserid = wb.Document.All["username"];
HtmlElement tbPasswd = wb.Document.All["password"];
tbUserid.SetAttribute("value", uname);
tbPasswd.SetAttribute("value", upass);
foreach (HtmlElement element in wb.Document.All)
if (element.TabIndex == 5) element.InvokeMember("click");
}
}
传递参数是正常的,就是遇到 操作 wb.Document 的时候 根本没有办法操作下去,也就无法完成自动填表,
请教各位这是为什么,有什么解决的方法么?
另外最好在DocumentCompleted事件里进行操作,以确保页面已经加载完成。
所以填表的时候webbrowser里尚未加载完成
填表工作要等到DocumentCompleted后再开始
thoughter,你好,webbrowser.navigate 是异步加载的,我把这个操作放在子线程里 sleep的,
所以不会睡上主线程,问题的关键是 无法获取 webbrowser.Document.Body.OuterHtml 里根本无法获取内容。