C# + WebBrowser如何实现淘宝页面的自动登录
我用DocumentCompleted来完成。
private static readonly string LOGIN_URL = "https://login.taobao.com/member/login.jhtml";
void TaobaoWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.ToString().Equals(LOGIN_URL))
{
HtmlElement name = TaobaoWebBrowser.Document.GetElementById("TPL_username_1");
if (name != null)
name.SetAttribute("value", "test");
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)TaobaoWebBrowser.Document.DomDocument;
mshtml.IHTMLElementCollection inputs = (mshtml.IHTMLElementCollection)doc.all.tags("INPUT");
foreach (mshtml.IHTMLElement ele in inputs)
{
if (ele.getAttribute("name", 0).Equals("TPL_password"))
ele.setAttribute("value", "test", 0);
}
//HtmlElement pwdSpan = TaobaoWebBrowser.Document.GetElementById("J_StandardPwd");
/*
foreach (HtmlElement child in pwdSpan.Children)
{
if (child.TagName.Equals("INPUT") && child.GetAttribute("name").Contains("TPL_password"))
{
((mshtml.IHTMLInputElement)child.DomElement).value = "test";
//child.SetAttribute("value", "test");
break;
}
}
*/
}
}
分别使用了C#托管和非托管两种方式做了实现。但是都无法在页面加载完后设置用户名和密码的输入框。高手指点,是什么问题?