C# ,webBrowser,登入,資料填充,類比點選連結,運行JS函數

來源:互聯網
上載者:User

判斷是否網路正常  

p rivate  bool IsConnectedToInternet()

        {

            int Desc;

            return internet.InternetGetConnectedState(out  Desc, 0);

        }   

設定預設頁面:

webBrowser1.Navigate(new Uri("http://www.ic2ic.com/copy.jsp"));

登入代碼:

      p rivate  void btnlogin_Click(object sender, EventArgs e)

        {

            webBrowser1.Document.GetElementById("id").InnerText =txtuser.Text.ToString().Trim();//fill name

            webBrowser1.Document.GetElementById("passwd").InnerText =txtpwd.Text.ToString().Trim();//fill pwd

            HtmlElement formLogin = webBrowser1.Document.Forms["frmLogin"];

         

            formLogin.InvokeMember("submit");

            Thread.Sleep(5000);

            herfclick("http://www.ic2ic.com/upload.jsp");//這是登入成功的操作,跳轉到//upload.asp

                                                         //注意不是直接跳過去的,類比點選連結

                                                         //SESSION不會丟失

            herfclick("http://www.ic2ic.com/copy.jsp"); 

        }

類比點選連結:

   p rivate void herfclick(string url)

        {

            for (int i = 0; i < webBrowser1.Document.All.Count; i++)

            {

                if (webBrowser1.Document.All[i].TagName == "A" && webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim() ==url)

                {

                    webBrowser1.Document.All[i].InvokeMember("click");//引發”CLICK”事件

                    break;

                }

            }

            

        }

運行網頁中的JS函數

    p rivate  void callJsMethod(string jsfun,string jsParameter)//函數名字,函數參數

        {

            HtmlDocument doc = webBrowser1.Document;

            doc.InvokeScript(jsfun,new object[]...{jsParameter});

        }

填充資料:

  p rivate void filldate(string txt)

        {

            HtmlElement frmWord = webBrowser1.Document.Forms["frmWord"];//找到from

            HtmlElementCollection txtarea = frmWord.GetElementsByTagName("TEXTAREA");

            HtmlElementCollection radio = frmWord.GetElementsByTagName("INPUT");

            radio[2].InvokeMember("click");//選擇radio控制項

            txtarea[0].InnerText = txt.ToString();

        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.