我用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#托管和非托管两种方式做了实现。但是都无法在页面加载完后设置用户名和密码的输入框。高手指点,是什么问题?

解决方案 »

  1.   

    HtmlElement name = TaobaoWebBrowser.Document.GetElementById("TPL_username_1");  if (name != null)
      name.SetAttribute("value", "test");经测试,用户名这里是可以自动填入的。但是密码部分就不行。
      

  2.   

    如果用安全控件,可以试试先设置控件的焦点,再用SendKeys来模拟输入。
      

  3.   

    试验成功,可以向安全控件输入文字。
                    var elt = webBrowser1.Document.All["Password_Edit_IE"];                if (elt != null)
                    {
                        elt.Focus();
                        SendKeys.Send("aaaa");
                        
                    }