一、获取网页源代码

1.不含有框架

string s=WB1.DocumentText;  //webbrowser1命名为WB1,下同

2.含有框架

引用mshtml;          //位置C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll

object i_frame=0;     //第一个框架

                IHTMLDocument2 doc = FWB.Document.DomDocument as IHTMLDocument2;

                IHTMLFramesCollection2 frames = doc.frames as IHTMLFramesCollection2;

IHTMLWindow2 frame = frames.item(ref i_frame) as IHTMLWindow2;

                IHTMLDocument2 frameDoc = frame.document as IHTMLDocument2;

string s=frameDoc.Document.body.innerHTML;

二、获取网页元素

1.根据ID

HtmlDocument ele=   WB1.Document.getElementById("元素的ID");

2,根据name

  HtmlDocument ele=   WB1.Document.all["元素的name"];

3.无ID无Name----遍历元素

例如:<input type="submit">提交</input>

string str_temp;

HtmlDocument ele=   WB1.Document.getElementById("元素的ID");

            foreach (IHTMLElement ele in eles)

            {

                if (ele.getAttribute("type", 0) != null)

                {

                    str_temp = ele.getAttribute("type", 0).ToString();

                    if (str_temp == "submit")

                    {

                        ele.click();

break;

                    }

                }

            }

三、遍历元素

HtmlDocument ele=   WB1.Document.getElementById("元素的ID");

1.遍历所有元素

HtmlDocument doc = WB1.Document;

            HtmlElementCollection elements = doc.All;

            foreach (HtmlElement element in elements)

            {

                if (element.GetAttribute("type") == "submit")

                {

                   element.InvokeMember("Click");

                    break;

                }

            }


2.根据元素类型 GetElementsByTagName

//目前有HTML、Form、Table、TR、TD、Div、A、 IMG、Li、Input、Span等

HtmlElementCollection eles = WB1.Document.GetElementsByTagName("li") as HtmlElementCollection;

            foreach (HtmlElement ele in eles)

            {

                if (ele.InnerText != null)

                {

                    if (ele.InnerText == "结婚饰品")

                    {

                        ele.InvokeMember("Click");

                        Application.DoEvents();

                    }

                }

            }

3.根据索引值

HtmlDocument ele=   WB1.Document.getElementsByTagName("input")[0];  //获取input类型的第一个元素

4.根据上下节点和父节点

例如:

获取已知ID的下一个节点

HtmlDocument ele=   WB1.Document.getElementById("元素的ID") .nextSibling;
//上个节点 previousSibling

获取已知ID的父节点的第1个节点

HtmlDocument ele=   WB1.Document.getElementById("元素的ID") .Parent.Children[0];
//或者firstChild

四、执行JS函数

1.用Navigate

WB1.Navigate("javascript:postComment();");    //postComment为要执行的JS函数

2.用IhtmlWindow2接口

IHTMLWindow2 win2 = WB1.Document.Window.DomWindow as IHTMLWindow2;

            win2.execScript("function confirm(){return true;}", "javascript");

3.用IhtmlDocument2接口的parentWindow

            IHTMLDocument2 doc2 = WB1.Document.DomDocument as IHTMLDocument2;

            doc2.parentWindow.execScript("function confirm() {return true;}", "javascript");

待续

c# webBrowser全掌握的更多相关文章

  1. 获取WebBrowser全cookie 和 httpWebRequest 异步获取页面数据

    获取WebBrowser全cookie [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true) ...

  2. C# webbrowser全掌握(二)

    全篇引用单元mshtml; 路径:C:\windows\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.msh ...

  3. webBrowser中操作网页元素全攻略

    原文 webBrowser中操作网页元素全攻略 1.获取非input控件的值: webBrowser1.Document.All["控件ID"].InnerText; 或webBr ...

  4. winform窗体控件(全)

    回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...

  5. CozyRSS开发记录6-继续补全订阅内容栏

    CozyRSS开发记录6-继续补全订阅内容栏 1.订阅内容栏布局 按照之前的原型图,把订阅内容栏分成三块.Xaml如下: 2.照葫芦画瓢,完成头部和列表 头部依然使用ColorZone,右侧再放两个按 ...

  6. .NET4.5 WFP中用WebBrowser获取/操作网页html代码

    引言 想给自己之前写的网页小说爬虫程序更新换代,之前一直是用winform的形式写的程序,因此这一次更新打算把UI换成WPF(因为听说WPF很漂亮),顺便也以此引入WPF的学习. 那么作为网页爬虫程序 ...

  7. delphi中webbrowser的用法

    WebBrowser1.GoHome; //到浏览器默认主页 WebBrowser1.Refresh; //刷新 WebBrowser1.GoBack; //后退 WebBrowser1.GoForw ...

  8. javascript小技巧(非常全)

    事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture();  event.srcE ...

  9. 基于Chromium构建Chrome WebBrowser for .net 控件(还有点心得体会)

    http://blog.csdn.net/lllllllllluoyi/article/details/8540054 首先向360说句sorry,在2011年360极速浏览器出现的时候我去他们论坛里 ...

随机推荐

  1. java_oop_类与对象

    类与对象包方法 ========================================= 对象的概念类与对象的关系    万物皆对象        属性(名词)            对象的 ...

  2. HTTP请求属性说明

    1)URL:页面地址. 2)Method :页面的提交方式,POST或GET. 3)EncType:编码类型.此参数给出一个内容类型(Content-Type),指定其做为回放脚本时“Content- ...

  3. R语言入门

    引入R的package(库) 首先是要安装TSA库,TSA是作者自己开发的一套基于R的pacakge,里面包含了函数以及数据:安装的方式是在R的控制台(console)中敲入install.packa ...

  4. ELK-head

    Head从elastic5开始只是支持单独服务器,不能够在通过 elasticsearch/bin/plugin -install mobz/elasticsearch-head 这样简单的方式安装插 ...

  5. MatConvNet中关于vl_simplenn_display的一些分析

    一.关于各层data size的分析 layer1:  floor((224-7)/2)+1=109 这里7是filter大小,2是stride layer4: floor((109+0+1-3)/2 ...

  6. Spring Cloud Eureka Server使用(注册中心)

    一.Spring Cloud Eureka 基于Netflix Eureka做了二次封装 由两个组件组成 Eureka Server 注册中心, 供服务注册的服务器 Eureka Client 服务注 ...

  7. 自然语言处理工具HanLP被收录中国大数据产业发展的创新技术新书《数据之翼》

    在12月20日由中国电子信息产业发展研究院主办的2018中国软件大会上,大快搜索获评“2018中国大数据基础软件领域领军企业”,并成功入选中国数字化转型TOP100服务商. 图:大快搜索获评“2018 ...

  8. php上传导入文件 nginx-502错误

    4. php程序执行时间过长而超时,检查nginx和fastcgi中各种timeout设置.(nginx 中的  fastcgi_connect_timeout 300;fastcgi_send_ti ...

  9. 多进程共享数据,真正的通信Manager

    Managers A manager object returned by Manager() controls a server process which holds Python objects ...

  10. 有了Jenkins,为什么还需要一个独立的部署系统

    需不需要一个独立的部署系统是很多企业用户在构建持续交付流程中经常困惑的一个问题.也经常有用户会问我们,现在已经有Jenkins,它自身提供了丰富的部署插件(如WebSphere部署插件.Tomcat部 ...