明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

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"是框架的name
webBrowser1.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"0as HTMLInputElementClass;
            input.value 
= "222an1";

            input 
= doc.all.item("passwd"0as 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;
                    }
                }
            }