WPF学习记录一 WebBrowser中操作源代码
2013-09-26 17:05
218 查看
本文收索自百度,已经无法确认正确出处,如作者看见,请知悉,我立刻注明出处 !
由于WPF的webBrowser控件与WINFORM的webBrowser控件非常的不一样,所以被折腾了一晚上,四处查阅相关资料,现将一些关键代码汇总一下。
首先要引入Microsoft.mshtml.dll 地址是C:\Program Files\Microsoft.NET\Primary Interop Assemblies
mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)webBrowser1.Document;
mshtml.IHTMLElement img = (mshtml.IHTMLElement)doc2.all.item("regimg", 0);
string imgUrl = (string)img.getAttribute("src");
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();
win.execScript("changeRegImg()", "javascript");//使用JS
{
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);
}
由于WPF的webBrowser控件与WINFORM的webBrowser控件非常的不一样,所以被折腾了一晚上,四处查阅相关资料,现将一些关键代码汇总一下。
首先要引入Microsoft.mshtml.dll 地址是C:\Program Files\Microsoft.NET\Primary Interop Assemblies
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);
}
相关文章推荐
- WPF中,在WebBrowser中操作源代码
- git gui简单操作 学习记录
- 【51单片机学习过程记录】7 中断之 定时计数器0 操作前理解过程
- 修改openssh源代码,添加操作记录审记功能
- perl学习(9) 实例:取出操作时间最长的100个记录
- Swift学习记录 -- 8.字典的基本操作
- redis学习记录(redis的持久化操作、基于java的jedis操作)
- wpf学习笔记(4)《都是自己根据网络资源学习记录的仅供参考》
- Hibernate学习-07:CRUD(增删改查)操作之查询多条/所有记录HQL、QBC、SQL三种方式
- 学习Bootstrap知识记录点-----按钮操作
- 使用zkclient操作zookeeper的学习过程记录
- WebBrowser 相关操作记录
- awk实例操作学习记录
- SAP BW学习第一课:BW建模——总账练习操作步骤记录
- WPF学习总结和记录(四)Items控件下
- CMake学习记录--list(列表操作命令)
- android学习记录(十三)Task 和 Activity 回退栈操作。
- django学习记录--在admin界面进行搜索操作后,报错:Related Field got invalid lookup: icontains
- web进阶之jQuery操作DOM元素&&MySQL记录操作&&PHP面向对象学习笔记
- Python学习记录——常用的IO操作及代替批处理命令