C# webbrowser全掌握(二)
全篇引用单元mshtml;
路径:C:\windows\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll //不同的版本路径不同
一、如何用Webbrowser获取网页的全部源代码
1.不含框架
string s= WB1.DocumentText;
2.含有框架
IHTMLDocument2 doc=WB1.Document.DomDocument as IHTMLDocument2;
IHTMLFramesCollection2 frames=doc.frames as IHTMLFramesCollection2;
int i_frame=0; //第1个框架
IHTMLWindow2 frame=frames.item(ref i_frame) as IHTMLWindow2;
IHTMLDocument2 doc2=frame.document as IHTMLDocument2;
IHTMLDocument3 doc3=frame.document as IHTMLDocument3;
strings = doc2.body.innerHTML;
二、webbrowser如何获取网页的元素
1.根据元素ID
HtmlElement ele= WB1.Document.GetElementById("ID");
2.根据元素Name
HtmlElement ele= WB1.Document.All["Name"];
3. 根据元素的索引序号
HtmlElement ele=WB1.Document.All[0]; //第1个元素
HtmlElement ele= WB1.Document.GetElementsByTagName("input")[0]; //第一个inuput类型的元素
4.无ID无Name的元素
4.1 webbrowser遍历网页所有元素:
HtmlDocument doc=WB1.Document;
HtmlElementCollection elements=doc.All;
foreach(HtmlElement element in elements)
{
if (element.GetAttribute("innerText") == "提交回答") //元素的innnerText属性为“提交回答”
{
element.InvokeMember("Click"); //模拟点击
break;
}
}
4.2 webbrowser 根据元素的Tag遍历(有HTML、Form、Table、TR、TD、Div、A、 IMG、Li、Input、Span等)
input: 所有可输入的类型,例如文本框、勾选框、复选框、下拉列表、图片等
a: A标签,可以带超链接
img: 图片类型
span: SPAN类型
li: 下拉列表,列表框等
div: DIV
HtmlElementCollection eles = WB1.Document.GetElementsByTagName("a") as HtmlElementCollection; //所有的A标签集合
foreach (HtmlElement ele in eles)
{
if (ele.GetAttribute("href") != null)
if (ele.GetAttribute("href") == "A标签的超链接")
{
element.InvokeMember("Click"); //模拟点击
break;
}
}
4.3 根据已知元素获取未知元素
例如:
获取已知ID的下一个节点
HtmlElement ele= WB1.Document.GetElementById("元素的ID") .NextSibling;
//上个节点 previousSibling获取已知ID的父节点的第1个节点
HtmlElement ele= WB1.Document.GetElementById("元素的ID") .Parent.Children[0];
//或者firstChild
三、webbrowser模拟填表
四、webbrowser执行JS函数
1.用Navigate
WB1.Navigate("javascript:alert('弹出信息!');"); //alert为要执行的JS函数
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");
{
e.Cancel=true; //屏蔽弹出到IE浏览器
// ppDisp=WB2.ActiveXInstance; //新建的webbrowser(WB2)窗口打开;
string url=WB1.Document.ActiveElement.GetAttribute("href");
if(url=="")
WB1.Navigate(url); //本窗口打开
}
Size sz=WB1.Size;
int xbili=(int)((float)sz.Width/(float)szb.Width*100);//水平方向缩小比例
int ybili=(int)((float)sz.Height/(float)szb.Height*100);//垂直方向缩小比例
WB1.Document.Body.Style="zoom:"+xbili.ToString()+"%";
WB1.Invalidate();
HtmlDocument document = WB1.Document;
int top = document.GetElementsByTagName("HTML")[0].ScrollTop;//滚动条垂直位置
指定滚动条滚动到指定位置
WB1.Document.Window.ScrollTo(0, 100);//滚动到100的位置
WB1.Document.Window.ScrollTo(0, WB1.Document.Body.ScrollRectangle.Height);//滚动到底部
七、Webbrowser遍历网页元素
//不引用其他单元
foreach(HtmlElement ele in WB1.Document.All)
{
if(ele.InnerText=="下一页>")
{
ele.InvokeMember("Click");
break;
}
}
//引用mshtml;
IHTMLDocument2 doc = WB1.Document.DomDocument as IHTMLDocument2;
foreach (IHTMLElement ele in doc2.all)
{
if (ele.innerText == "下一页>")
{
ele.click();
break;
}
}
C# webbrowser全掌握(二)的更多相关文章
- 获取WebBrowser全cookie 和 httpWebRequest 异步获取页面数据
获取WebBrowser全cookie [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true) ...
- Xcode括号自动补全以及二次编译后不显示输入
今天遇到了一个大坑,在使用栈来进行计算表达式的时候,发现输入括号就报错,以及二次编译后不显示. 测试了好久,经过无数次debug后. 二次编译不显示还是没搞明白,不过输入倒是没什么问题,就是不显示出来 ...
- 通过trie树单词自动补全(二)
经常使用iciba进行单词查询, 关于他的搜索建议是通过单词前缀做的索引, 所以自己想动手实现下, 当然如果借助mysql的话,一条sql语句就能实现, 网上查询了下trie正适合做这个,所以通过C语 ...
- c# webBrowser全掌握
一.获取网页源代码 1.不含有框架 string s=WB1.DocumentText; //webbrowser1命名为WB1,下同 2.含有框架 引用mshtml; //位置C ...
- Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(二)
用mogoose搭建restful测试接口 接着上一篇(Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一))记录,今天单独搭建一个restful测试接口,和项目前 ...
- C# WebBrowser使用 网络(二)
WebBrowser 简单操作 Form 代码 public partial class Form1 : Form { public Form1() { InitializeComponent(); ...
- DL基础补全计划(二)---Softmax回归及示例(Pytorch,交叉熵损失)
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明 本文作为本人csdn blog的主站的备份.(Bl ...
- Sql Server函数全解<二>数学函数
阅读目录 1.绝对值函数ABS(x)和返回圆周率的函数PI() 2.平方根函数SQRT(x) 3.获取随机函数的函数RAND()和RAND(x) 4.四舍五入函数ROUND(x,y) 5.符号函数SI ...
- Sql Server函数全解(二)数学函数
数学函数主要用来处理数值数据,主要的数学函数有:绝对值函数,三角函数(包括正弦函数,余弦函数,正切函数,余切函数).对数函数,随机函数等.在错误产生时,数学函数将返回空值null.本次介绍各种数学 ...
随机推荐
- The key of real time embedded system
对于实时嵌入式系统来说,最重要的是每一个进程所需时间的可检测性,可预测性.要不你的实时性是没有办法保证的.有些时候你对一些没有从事过嵌入式开发的人谈这个进程(TASK)设计是按8ms被调度一次,他们会 ...
- 利用反射--调用一个按钮的Click事件
最基本的调用方法 (1)button1.PerformClick();(2)button1_Click(null,null);(3)button_Click(null,new EventArgs()) ...
- C#遍历菜单项
(1)横向遍历 ToolStripMenuItem foreach (ToolStripMenuItem con in this.MainMenuStrip.Items) { ...
- chrome浏览器好用的插件
1.Chrome批量保存所有选项卡网址 + 批量打开复制网址小插件 批量保存所有选项卡网址插件:Copy All Urls 经常搜索一些东西,下班时无法处理完所有网页内容,比如做笔记什么的,又不舍得关 ...
- day5 大纲
01 昨日内容回顾 list: 增: append insert(index,object) extend() 迭代着追加 删: pop(默认删除最后一个)按照索引去删除 有返回值 remove 按照 ...
- linux下常见软件安装
读者还可以参考文档:https://download.csdn.net/download/qq_27799563/10482900 Mysql的安装过程: 解压MySQL安装包: tar -xvf M ...
- bzoj 4184 shallot——线段树分治+线性基
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4184 本来想了可持久化trie,不过空间是 nlogn (出一个节点的时候把 tot 复原就 ...
- 全文检索Solr集成HanLP中文分词
以前发布过HanLP的Lucene插件,后来很多人跟我说其实Solr更流行(反正我是觉得既然Solr是Lucene的子项目,那么稍微改改配置就能支持Solr),于是就抽空做了个Solr插件出来,开源在 ...
- RedHat6.5安装Spark集群
版本号: RedHat6.5 RHEL 6.5系统安装配置图解教程(rhel-server-6.5) JDK1.8 http://blog.csdn.net/chongxin1/arti ...
- 为什么人工智能用Python
开发效率高.python有很多库很方便做人工智能,比如numpy, scipy做数值计算的,sklearn做机器学习的,pybrain做神经网络的,matplotlib将数据可视化的. 如果很在乎性能 ...