C#表单 - 登录WebSite

时间:2014-01-13 09:51:56

标签: c# php

我想从C#表单登录网站,我找到用户文本框ID和密码文本框ID,但我找不到登录按钮ID。 当按钮在网站上没有id并且我想从C#表单登录时我该怎么办?

webBrowser1.Document.GetElementById("user_name").SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementById("user_password").SetAttribute("value", textBox2.Text);
webBrowser1.Document.GetElementById("here i wont button id").InvokeMember("click");

1 个答案:

答案 0 :(得分:0)

如果您找不到该登录按钮的唯一内容:

编辑:已测试并正常工作

private void Form1_Load(object sender, EventArgs e)
        {
            //WebBrowser webBrowser1 = new WebBrowser();
            webBrowser1.Navigate("http://www.facebook.com");

            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(loaded);


        }


        private void loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var bro = sender as WebBrowser;
            var List = bro.Document.GetElementsByTagName("input");



            foreach (HtmlElement Item in List)
            {
                String Value = Item.GetAttribute("value");
                if(Value != null && Value == "Log In")
                {
                    MessageBox.Show(Value);

                    bro.Document.GetElementById("email").SetAttribute("value", "Some Email");
                    bro.Document.GetElementById("pass").SetAttribute("value", "Some Pass");

                    Item.InvokeMember("click");
                    // avoid Repetition
                    bro.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(loaded);
                }
            }
        }

我希望这有帮助。