asp.net WebBrowser操作網頁代碼

來源:互聯網
上載者:User

使用fiddle截獲post給伺服器的url和資料,使用httprequest類代碼post到伺服器
今天給出另外一種方法,就是利用winform控制項中的webbrowser控制項來操作頁面。

這裡我們會類比一個搜尋過程,開啟百度網站,輸入搜尋索引鍵,在搜尋結果中開啟串連

 代碼如下 複製代碼
public form1()
        {
            initializecomponent();
            (webbrowser1.activexinstance as shdocvw.webbrowser).navigatecomplete2 +=
                new shdocvw.dwebbrowserevents2_navigatecomplete2eventhandler(form1_navigatecomplete2);
        }
        void form1_navigatecomplete2(object pdisp, ref object url)
        {
            ihtmldocument2 doc = (webbrowser1.activexinstance as shdocvw.webbrowser).document as ihtmldocument2;
            doc.parentwindow.execscript("window.alert=null", "網頁特效");
            doc.parentwindow.execscript("window.confirm=null", "javascript");
            doc.parentwindow.execscript("window.open=null", "javascript");
            doc.parentwindow.execscript("window.showmodaldialog=null", "javascript");
            doc.parentwindow.execscript("window.close=null", "javascript");
        }


代碼寫到這裡已經提交搜尋了,webbrowser也會接收這次提交後的記過,下面我們需要在結果中開啟一個串連做實驗。
先查看一下搜尋結果的html原始碼

 代碼如下 複製代碼
private void webbrowser1_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e)
        {
            htmldocument doc = this.webbrowser1.document;
            if(doc.url.absoluteuri=="http://www.baidu.com/")
            {
                doc.getelementbyid("kw").innertext = "翁玉禮";
                doc.getelementbyid("su").invokemember("click");   
            }           
            if(doc.url.absoluteuri.contains("wd"))//根據url中的
            {
                htmlelement table = doc.getelementbyid("1");//根據搜尋結果的html表,找到第一個搜尋結果的url,這裡類比
                var html = table.children[0].children[0].innerhtml;
                var url = html.substring(html.indexof("href") + 6, html.indexof(" target"));
                this.webbrowser1.navigate(url);
            }
        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.