一、获取网页源代码

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. C#模拟键盘按键的三种方式实现

    1.System.Windows.Forms.SendKeys 组合键:Ctrl = ^ .Shift = + .Alt = % 模拟按键:A private void button1_Click(o ...

  2. kettle的系列教程

    kettle--window开发环境和linux运行环境的迁移 kettle的输入输出组件和脚本组件 kettle--变量的使用 kettle的资源库 kettle的job kettle的hello ...

  3. 设置 IntelliJ IDEA 的彩色代码主题

    首先,给出一系列 IntelliJ IDEA 代码的彩色主题,供大家选择: VibrantUnknown(Darcula) FadeComments NicePython Solarized Have ...

  4. 电脑上不安装Oracle时,C# 调用oracle数据库,Oracle客户工具 【转载】

    http://www.cnblogs.com/jiekzou/p/5047850.html Oracle的安装包通常都比较大,安装又比较费时,而且如果安装过程中不幸出错,各种蛋疼,即便是安装过N遍的老 ...

  5. ML(4)——逻辑回归

    Logistic Regression虽然名字里带“回归”,但是它实际上是一种分类方法,“逻辑”是Logistic的音译,和真正的逻辑没有任何关系. 模型 线性模型 由于逻辑回归是一种分类方法,所以我 ...

  6. ML(2)——感知器

    感知器(PLA——Perceptron Learning Algorithm),也叫感知机,处理的是机器学习中的分类问题,通过学习得到感知器模型来对新实例进行预测,因此属于判别模型.感知器于1957年 ...

  7. e生保plus

    e生保plus https://m.health.pingan.com/share/products/esb_plus.html?re_from=qdlmMSDbxtj&order_from= ...

  8. React Native 学习资料

    React Native 学习资料 学习资料 网址 React Native中文网 https://reactnative.cn/

  9. java中==与equals

    == ==可用于比较基本类型与引用类型,对于基本类型变量比较的是其存储的值是否相等,对于引用类型则比较的是其是否指向同一个对象. 如: int a = 10; int b = 20; double d ...

  10. Jmeter -- 属性和变量

    一.Jmeter中的属性: 1.JMeter属性统一定义在jmeter.properties文件中,我们可以在该文件中添加自定义的属性 2.JMeter属性在测试脚本的任何地方都是可见的(全局),通常 ...