C# :webBrowser 框架頁 – 賦值 – 點擊事件 – 框架頁中下拉框賦值

來源:互聯網
上載者:User

net框架頁中自動插入資料 ,網上搜集了一些資料

首先對webBrowser載入網頁

this.webBrowser1.Url = new System.Uri("url地址", System.UriKind.Absolute);

給一般不是框架頁中的文字框賦值webBrowser1.Document.GetElementById("文字框ID").InnerText = "weiling";//文字框賦值根據ID賦值
或者:this.webBrowser1.Document.All["文字框name"].SetAttribute("value", "0924");//文字框賦值根據name賦值

表單提交,也可以看成是一個點擊事件 HtmlElement form = webBrowser1.Document.GetElementById("formID");//提交表單
 form.InvokeMember("submit");

框架頁中的文字框賦值,"frameMain"是架構的namewebBrowser1.Document.Window.Frames["frameMain"].Document.GetElementById("txtXingming").InnerText = "521656";//架構賦值
注:frameMain 是架構的name

框架頁中下拉框賦值HtmlDocument doc = webBrowser1.Document.Window.Frames["frameMain"].Document;//架構下下拉框賦值
HtmlElement el = doc.GetElementById("drpXingbie");
el.SetAttribute("selectedIndex", "1");

 

框架頁中文字框賦值
代碼

   HtmlElement tbUserid = webBrowser1.Document.Window.Frames[0].Document.All["username"];
            tbUserid.SetAttribute("value", "k221");

            HtmlElement tbPasswd = webBrowser1.Document.Window.Frames[0].Document.All["passwd"];
            tbPasswd.SetAttribute("value", "33311");  

            HtmlDocument htmlDoc = webBrowser1.Document.Window.Frames[0].Document;
            foreach (HtmlElement h in htmlDoc.All)
            {
                if (h.GetAttribute("type").ToString() == "submit")
                {
                    if (h.GetAttribute("value").ToString() == "確定")
                    {
                        h.InvokeMember("click");
                        break;
                    }
                }
            }

 

 

 擷取架構內Html頁面內容

1.擷取frame的源檔案

MessageBox.Show(webBrowser1.Document.Window.Frames["main"].Document.Body.InnerHtml);

2.擷取frame的HTMLDocument介面

HTMLDocument doc = (HTMLDocument)webBrowser1.Document.DomDocument;
object j;
for (int i = 0; i < doc.parentWindow.frames.length; i++)
{
j = i;
HTMLWindow2Class frame = doc.parentWindow.frames.item(ref j) as HTMLWindow2Class;
if (frame.name == "main")
{                   
MessageBox.Show(frame.document.title);                    
}
}      

3.擷取frame的IHTMLDocument2介面

IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.Window.Frames["main"].Document.DomDocument;

4.取得frame中被點擊的串連

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
string url = webBrowser1.Document.Window.Frames["main"].Document.ActiveElement.GetAttribute("src");
}

 

 

 

webBrowser1.Navigate(new Uri("http://asd10000.com/"));

webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;  //'屏蔽右鍵菜單

 

代碼

 if (webBrowser1.Document.Window.Frames == null)
                return;

            HTMLDocument doc = webBrowser1.Document.DomDocument as HTMLDocument;
            HtmlWindow window = webBrowser1.Document.Window.Frames[0];
            doc = window.Document.DomDocument as HTMLDocument;

            HTMLInputElementClass input = doc.all.item("username", 0) as HTMLInputElementClass;
            input.value = "222an1";

            input = doc.all.item("passwd", 0) as HTMLInputElementClass;
            input.value = "222111";

            HtmlDocument htmlDoc = webBrowser1.Document.Window.Frames[0].Document;
            foreach (HtmlElement h in htmlDoc.All)
            {
                if (h.GetAttribute("type").ToString() == "submit")
                {
                    if (h.GetAttribute("value").ToString() == "確定")
                    {
                        h.InvokeMember("click");
                        break;
                    }
                }
            }

 

 

 

 

 

相關文章

聯繫我們

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