c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 .
c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2
可以实现例如通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;可以自动点击网页上的按钮等功能
1. 加入对Microsoft Internet Controls的引用;
2. 加入对Microsoft HTML Object Library的引用;
(要引入Microsoft.mshtml.dll 地址是C:\Program Files\Microsoft.NET\Primary Interop Assemblies)
3. 通过mshtml.IHTMLDocument2、SHDocVw.InternetExplorer、SHDocVw.ShellWindowsClass获取当前打开的google搜索页面的IE窗口句柄;
4. 根据3返回的句柄,获得当前打开的google页面的mshtml.IHTMLDocument2对象;
5. 根据4返回的IHTMLDocument2对象,获得搜索输入框和提交按钮(可查看google页面源文件,确认输入框和提交按钮的类型和名字);
6. 在搜索输入框中输入要搜索的内容,并执行提交按钮的click动作即可进行搜索;
简单来说:
打开ie:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
object objFlags = 1;
object objTargetFrameName = "";
object objPostData = "";
object objHeaders = "";
SHDocVw.InternetExplorer webBrowser1= (SHDocVw.InternetExplorer)shellWindows.Item(shellWindows.Count-1);
webBrowser1.Navigate(“http://www.google.cn”, ref objFlags, ref objTargetFrameName, ref objPostData, ref objHeaders);
(可以简略点写:object c=null; myWeb.Navigate("http://zhidao.baidu.com/",ref c,ref c,ref c,ref c); )
mshtml.IHTMLDocument2 htmlDoc = webBrowser1.Document as mshtml.IHTMLDocument2;
//...获取WebBroswer中的body代码
mshtml.HTMLDocumentClass doc=(mshtml.HTMLDocumentClass)myWeb.Document;
mshtml.HTMLBody body=(mshtml.HTMLBody)docCC.body;
string html=body.innerHTML.ToString();
//...如果里面有Form,要给里面的text填充信息
mshtml.IHTMLDocument2 doc2=(mshtml.IHTMLDocument2)myWeb.Document;
mshtml.IHTMLElementCollection inputs;
inputs=(mshtml.IHTMLElementCollection)doc2.all.tags("INPUT");
mshtml.IHTMLElement element=(mshtml.IHTMLElement)inputs.item("userName",0);
mshtml.IHTMLInputElement inputElement=(mshtml.IHTMLInputElement)element;
inputElement.value="填充信息";
//...要点击里面的某个按钮
mshtml.IHTMLDocument2 doc2=(mshtml.IHTMLDocument2)myWeb.Document;
mshtml.IHTMLElementCollection inputs;
inputs=(mshtml.IHTMLElementCollection)doc2.all.tags("INPUT");
mshtml.IHTMLElement element=(mshtml.IHTMLElement)inputs.item("SubmitBut",0);
element.click();
1、根据元素ID获取元素的值。
比如要获取<img class="" id="regimg" src="/register/checkregcode.html?1287068791" width="80" height="22">这个标签里的src属性的值:
mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)webBrowser1.Document;
mshtml.IHTMLElement img = (mshtml.IHTMLElement)doc2.all.item("regimg", 0);
string imgUrl = (string)img.getAttribute("src");
2、填写表单,并确定
mshtml.IHTMLElement loginname = (mshtml.IHTMLElement)doc2.all.item("loginname", 0);
mshtml.IHTMLElement loginPW = (mshtml.IHTMLElement)doc2.all.item("password", 0);
mshtml.IHTMLElement loginBT = (mshtml.IHTMLElement)doc2.all.item("formsubmit", 0);
mshtml.IHTMLElement loginYZ = (mshtml.IHTMLElement)doc2.all.item("regcode", 0);
loginname.setAttribute("value", tbLoginName.Text);
loginPW.setAttribute("value", tbLoginPassWord.Password);
loginYZ.setAttribute("value", tbYZ.Text);
loginBT.click();
3、获取源码
textBox1.Text = doc2.body.innerHTML;
4、执行JS
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;
win.execScript("changeRegImg()", "javascript");//使用JS
5、禁止JS,WPF下目前发现唯一适用的一种方法:
public void HideScriptErrors(WebBrowser wb, bool Hide)
{
FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
if (fiComWebBrowser == null) return;
object objComWebBrowser = fiComWebBrowser.GetValue(wb);
if (objComWebBrowser == null) return;
objComWebBrowser.GetType().InvokeMember(
"Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
}
void webBrowser1_Navigated(object sender, NavigationEventArgs e)
{
HideScriptErrors(webBrowser1,
true);
}
下面是另外一遍博客里写的比较好的
#region Search
public static void Search(string searchText)
{
SHDocVw.InternetExplorer ieWnd = GetIEWndOfGoogle();
mshtml.IHTMLDocument2 ieDoc = GetIEDocOfGoogle(ref ieWnd);
System.Diagnostics.Trace.Assert(ieDoc != null);
SearchTextInGoogle(ieDoc, searchText);
//activate ie window
SetForegroundWindow(ieWnd.HWND);
}
#endregion
#region get ie window of google page
public static SHDocVw.InternetExplorer GetIEWndOfGoogle()
{
mshtml.IHTMLDocument2 ieDoc;
SHDocVw.InternetExplorer ieWnd = null;
SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
//if it is ie window
if (ie.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)
{
//get the document displayed
ieDoc = (mshtml.IHTMLDocument2)ie.Document;
if (ieDoc.title.ToUpper().IndexOf("GOOGLE") >= 0)
{
ieWnd = ie;
break;
}
}
}
shellWindows = null;
return ieWnd;
}
#endregion
#region get ie document of google page
public static mshtml.IHTMLDocument2 GetIEDocOfGoogle(ref SHDocVw.InternetExplorer ieWnd)
{
object missing = null;
mshtml.IHTMLDocument2 ieDoc;
if (ieWnd == null)
{
ieWnd = new SHDocVw.InternetExplorer();
ieWnd.Visible = true;
ieWnd.Navigate("http://www.google.com", ref missing, ref missing, ref missing, ref missing);
//wait for loading completed, or using DocumentComplete Event
while (ieWnd.StatusText.IndexOf("完成") == -1)
Application.DoEvents();
}
ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document;
return ieDoc;
}
#endregion
#region Search the given text in google
/ <summary>
/// search the given text in google home page
/// we can see the source file of google home page to confirm the elements we need
/// the html file of google home page is as follows
///
/// <table cellpadding=0 cellspacing=0>
/// <tr valign=top>
/// <td width=25%> </td>
/// <td align=center nowrap>
/// <input name=hl type=hidden value=zh-CN>
/// <input autocomplete="off" maxlength=2048 name=q size=55 title="Google 搜索" value="">
/// <br>
/// <input name=btnG type=submit value="Google 搜索">
/// <input name=btnI type=submit value=" 手气不错 ">
/// </td>
/// ...
/ </summary>
public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText)
{
mshtml.HTMLInputElementClass input;
//set the text to be searched
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input and name is q(question)
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = ((mshtml.HTMLInputElementClass)ieElement);
if (input.name == "q")
{
input.value = searchText;
break;
}
}
}
//click the submit button to search
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = (mshtml.HTMLInputElementClass)ieElement;
if (input.name == "btnG")
{
input.click();
break;
}
}
}
}
#endregion
参考文章:
http://blog.csdn.net/livelylittlefish/archive/2008/08/25/2829873.aspx
http://hi.baidu.com/andyleesoft/blog/item/802e02289fcc1f94023bf66a.html
http://zhidao.baidu.com/question/48084010.html
另外一个例子
IHTMLDocument2 doc = webbrowser.Document.DomDocument as IHTMLDocument2;
IHTMLBodyElement bodyElement = doc.body as IHTMLBodyElement;
if (bodyElement != null)
{
IHTMLTxtRange range = bodyElement.createTextRange();
HTMLDocumentClass documentClass = wb1.Document.DomDocument as HTMLDocumentClass;
IHTMLElement caret_pos = documentClass.getElementById("caret_pos");
if (caret_pos != null)
{
range.moveToElementText(caret_pos);
range.select();
}
}
2011-3-14添加
给html元素添加事件
IHTMLElement ieElement = .......
((mshtml.HTMLElementEvents2_Event)ieElement).onclick += new mshtml.HTMLElementEvents2_onclickEventHandler(this.element_onClick);
public bool element_onClick(mshtml.IHTMLEventObj e)
{。。。。}
要在程序中出发html中的事件,这个想用什么eventhandler之类的,搞了半天都没有研究出来,资料又搜不到,最后用执行js的方法实现之:
js触发事件:
var oEvent = document.createEventObject();
document.getElementById('addrCity').fireEvent('onchange', oEvent);
转载于:https://www.cnblogs.com/xishi/p/4374757.html
c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 .相关推荐
- c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2
c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 原文:c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 ...
- javascript脚本实现浏览器自动点击(阿里员工秒杀月饼)
原文地址https://blog.csdn.net/ani521smile/article/details/52575063 秒杀活动页面 <!DOCTYPE HTML> <html ...
- js实现浏览器自动点击
<!DOCTYPE HTML> <html><head><meta http-equiv="Content-Type" content=& ...
- 【Nodejs】使用robotjs控制鼠标键盘 自动点击屏幕上指定位置的图标 实现连接wifi等操作
每天上班开机挺麻烦,要手动连wifi:因此可以写一个很简单的自动执行脚本,执行 node xxxxxx.js 安装 robotjs npm i robotjs -g xxxxxx.js 以下数字自行根 ...
- S脚本实现浏览器自动点击(阿里员工秒杀月饼)
秒杀活动页面 <!DOCTYPE HTML> <html><head><meta http-equiv="Content-Type" co ...
- 自动点击触发事件(自动按下释放键盘上某个键)
Robot类用于为测试自动化.自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件.Robot 的主要目的是便于 Java 平台实现自动测试. 使用该类生成输入事件与将事件发送到 AW ...
- html怎么自动点击按钮事件,JS按钮点击事件自动运行的问题?
如下是我学习时写的代码,但是一运行网页JS里面的那几个按钮事件就自动运行了,不知道我哪里搞错了,我在百度上找了看到按钮事件是像这样写的呀.请大能帮帮小弟,看小弟是哪里搞错了,谢谢哈. 代码: Page ...
- html 自动点击回车事件,1秒自动按回车键的脚本
以下存为BAT文件.双击安装一次脚本,以后可以通过快捷键调用f10开始.f11停止 @echo off title 每隔1秒同时按下回车 mode con: cols=50 lines=5 color ...
- 安卓自动点击器和iOS切换控制在移动端测试中的应用
在移动端测试工作中有一些需要长时间进行简单且重复操作的场景,这种情况可以借助一些简单的小工具来实现自动化点击操作.能够应用以下工具的场景需要满足两个条件:1.操作简单,只涉及一些点击.滑动.长按.双击 ...
- [html] 如何解决微信浏览器视频点击自动全屏的问题?
[html] 如何解决微信浏览器视频点击自动全屏的问题? 1.1 页面内播放 X5内核视频在用户点击后默认会进入全屏播放,前端可以设置video的x5-playsinline属性来将视频限定于网页内部 ...
最新文章
- java-第十一章-类的无参方法-随机出一个商品规定次数猜对商品归用户所有
- Android 实现网页账号自动登录
- html表单php比较三个值大小,PHP比较三个数大小实现办法
- wpf mvvm模式下CommandParameter传递多参
- java以Blob形式存储,读取图片并在jsp页面显示图片流
- 数据库原理及应用【二】数据模型
- 云服务器 自有操作系统,云服务器 自有操作系统
- 已解决:home目录下ubuntu文件夹被误删。。。。
- python基础之列表、元组和字典
- 拓端tecdat|爬取微博用户行为数据语义分析数据挖掘报告
- 毕业设计-基于Javaweb实现超市管理系统
- 新版谷歌浏览器80.0永久开启Flash
- IOTQQ(OPQbot)—QQ机器人、部署在linux上(一步步实
- 智慧路灯控制系统解决方案
- 图片怎样放大后不模糊?
- 深度学习用于股票预测_用于自动股票交易的深度强化学习
- 个人永久性免费-Excel催化剂功能第60波-数据有效性验证增强版,补足Excel天生不足...
- 一文读懂闪电网络工作原理
- SSM之spring事务管理
- 项立刚:FDD牌照发放 难改行业大格局
热门文章
- Android客户端和服务器端数据交互的第二种方法
- 44. 扑克牌的顺子(C++版本)
- IDEA创建javaweb项目,及常见的请求和响应头
- 子过程或函数未定义_Power Pivotamp;Power BI DAX函数说明速查
- 字模提取软件怎么放大_图片无损放大软件 Topaz Gigapixel AI
- php post重复提交session,PHP加Session防止表单重复提交的解决方法
- html表格编辑器退出编辑状态,易优后台编辑器取消html标签(比如表格属性等)过滤解决方法...
- ubuntu 20.04命令行模式_Ubuntu18.04LTS升级到20.04LTS
- SQL:postgresql中在查询结果中将字符串转换为整形或浮点型
- JavaScript:对象转换为字符串、字符串转换为对象