当前位置:编程学习 > C#/ASP.NET >>

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#托管和非托管两种方式做了实现。但是都无法在页面加载完后设置用户名和密码的输入框。高手指点,是什么问题? --------------------编程问答-------------------- --------------------编程问答-------------------- HtmlElement name = TaobaoWebBrowser.Document.GetElementById("TPL_username_1");

  if (name != null)
  name.SetAttribute("value", "test");

经测试,用户名这里是可以自动填入的。但是密码部分就不行。 --------------------编程问答-------------------- 搞定。安全控件登陆的问题。取消安全控件登陆就可以了 --------------------编程问答-------------------- 如果用安全控件,可以试试先设置控件的焦点,再用SendKeys来模拟输入。 --------------------编程问答-------------------- 试验成功,可以向安全控件输入文字。


                var elt = webBrowser1.Document.All["Password_Edit_IE"];

                if (elt != null)
                {
                    elt.Focus();
                    SendKeys.Send("aaaa");
                    
                }
--------------------编程问答-------------------- 正愁登陆不了呢!感谢!!!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,