webBrower控件实现winform和webpage交互
添加WebBrowser控件
private WebBrowser webBrowser1;
引用页面的document对象
HtmlDocument doc = webBrowser1.Document;//get web document
有了document对象,就可以像js一样操作doc,访问页面的所有对象。
HtmlElementCollection htmlElements = webBrowser1.Document.GetElementsByTag("input");//get all input elements
//access every input element in web form
foreach (HtmlElement el in htmlElements)
{
strInputName = el.GetAttribute("name").ToString();//get input element's name
strInputValue = el.GetAttribute("value").ToString();//get input element's value
}
winForm调用webpage的函数
/*web page function*/
<script>
function jsMethod(var jsParam)
{
alert(param);
}
</script>
/*call jsMethod from winForm*/
private void callJsMethod(string Param)
{
HtmlDocument doc = webBrowser1.Document;
doc.InvokeScript("jsMethod",new object[]{"called by winForm"});
}
webPage调用winForm方法
//winform code
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]//
[System.Runtime.InteropServices.ComVisibleAttribute(true)]//This property lets you integrate dynamic HTML (DHTML) code with your client application code
public partial class Form2 : Form
{
public void winFormMethod(string param)
{
MessageBox.Show(param);
}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.ObjectForScripting = this;//important
}
}
//web page code
<input name="callWinMethod" onclick="window.external.winFormMethod('called from DHTML')">
要调用winform的方法,这两个属性是必须的
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
还有必须设置webBrowser1.ObjectForScripting = this,被调用的方法是public的。
有了上面这些准备要实现一些简单应用就很简单啦,不妨自己动手试试。
实例一
下面结合一个简单例子,使用webbrowser自动登录。
先分析webform的结构,下面这个登录页面包括两个输入框:用户名和密码,以及一个登录按钮。
<HTML>
<HEAD>
<title>test html</title>
</HEAD>
<body background="/bugnet/graphics/back2.gif">
<form name="mainform" method="post" action="bugl_login.aspx" id="mainform" >
<b>Enter name</b><input id="uid" type="text" maxLength="50" size="25" name="uid"><br>
<b>Enter Password</b><input type="password" maxLength="20" size="25" name="pwd">
<input type="submit" value="go" name="go">
</form>
</body>
</HTML>
在页面载入webbrowser之后,程序自动填充用户名和密码,触发登陆按钮。
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string strUID = "userName@sdccn.com";
string strPWD = "PWD";
webBrowser1.Document.GetElementById("uid").InnerText = strUID;//fill name
webBrowser1.Document.GetElementById("pwd").InnerText = strPWD;//fill pwd
webBrowser1.Document.GetElementById("go").InvokeMember("click");//click go
}
自动登录就这样实现,利用这些可以完成一些重复登录工作,还可以使用来自动化测试webpage程序。
实例二
抓取页面数据,下面的页面有一个表格,如何把里面的数据提取出来?
看看页面DOM结构,一个table,三行两列
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<TABLE border=1>
<TR>
<TD>name</TD>
<TD>age</TD>
<TD>score</TD>
</TR>
<TR>
<TD>agan</TD>
<TD>18</TD>
<TD>99</TD>
</TR>
<TR>
<TD>asca</TD>
<TD>18</TD>
<TD>88</TD>
</TR>
</TABLE>
</BODY>
</HTML>
了解这个表格结构就可以开始导入到winform中的DataTable中,然后在DataGridView中展示出来
private DataTable ImportToDataTable()
{
HtmlElementCollection htmlTabs = webBrowser1.Document.GetElementsByTagName("table");//get all tables in the dom
DataTable dt = null;
DataRow dr = null;
string strValue = "";
int intII=0;
if(htmlTabs!=null&&htmlTabs.length>0)
{
HtmlElement htmlTable = htmlElements[0];
HtmlElementCollection htmlRows = htmlElement.GetElementsByTagName("tr");//get all rows
HtmlElementCollection htmlCells = null;
foreach (HtmlElement htmlRow in htmlRows)
{
if (htmlRow == htmlRows[0])//build table header
{
BuildHeader(ref dt, htmlCells)
}
else
{
htmlCells = htmlRow.GetElementsByTagName("td");
dr = dt.NewRow();
foreach (HtmlElement htmlCell in htmlCells)
{
if (htmlCell.InnerText!=null)
{
strValue = htmlCell.InnerText.Trim();
dr[intII++] = strValue;
}
}
dt.Rows.Add(dr);
}
}
}
return dt;
}
private void BuildHeader(ref DataTable dt, HtmlElementCollection htmlCells)
{
int intCols = htmlCells.Count;
if (dt == null)
{
dt = new DataTable();
for (int i = 0; i < intCols; i++)
dt.Columns.Add("col" + i, Type.GetType("System.String"));
}
}
例子对导入的数据简单的以string来处理,其实可以做一些深入处理,比如使用正则表达式识别不同的数据类型,希望这个例子能起到抛砖引玉的作用。
webBrower控件实现winform和webpage交互的更多相关文章
- webbrowser 控件实现WinForm与WebForm交互
WebBrowser 控件可以让你装载Windows Form 应用程序中的 Web 网页和其它采用浏览器的文件.可以使用webbrowser 控件将现有的web框架控制项加入至 Windows Fo ...
- C# 网络编程之网页自动登录 (一).使用WebBrower控件模仿登录
最近学习C#网络编程中,想实现网页自动登录并提交GET/POST信息,再实现循环登录不断发送报文给服务器,服务器发送消息给客户端记录能登录的账户和密码,做到后面实现绕过验证码.动态抓取登录位置等,但由 ...
- Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题)
Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题) 2016-12-04 20:11 362人阅读 评论(4) 收藏 举报 分类: Halco ...
- C#常见控件与SQL Sever数据库交互
C#常见控件与SQL Sever数据库交互 下拉框(ComboBox)与数据库绑定 首先,我们采用DataSet作为临时的数据库,这样会比较好 那么,我们先创建两个成员(对象) string sqlc ...
- 浏览器自动化的一些体会5 webBrowser控件之winform和webBrowser的交互
从winform访问webBrowser,大致就是利用webBrowser提供的解析dom的方法以及用InvokeScript方法执行javascript.这个相对比较简单. 从webBrowser访 ...
- WPF控件--利用Winform库中的NotifyIcon实现托盘小程序
WPF控件--NotifyIcon 运行界面如下所示: 图1 图2 代码很少,如下所示 ...
- 自己写的一个分页控件类(WinForm)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
- jquery文件上传控件 Uploadify 可以和ajax交互
http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html 原网址 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...
- Winform控件WebBrowser与JS脚本交互
1)在c#中调用js函数 如果要传值,则可以定义object[]数组. 具体方法如下例子: 首先在js中定义被c#调用的方法: function Messageaa(message) { ...
随机推荐
- sar使用说明
sar这东西,一开始还以为是内部有的,原来是外部的工具,可以到 http://pagesperso-orange.fr/sebastien.godard/download.html 去下载 1 安装 ...
- 获取webshell的十种方法
黑客在入侵企业网站时,通常要通过各种方式获取webshell从而获得企业网站的控制权,然后方便进行之后的入侵行为.本篇文章将如何获取webshell总结成为了十种方法,希望广大的企业网络管理员能够通过 ...
- Working with forms
翻译 Django文档 Version 1.5 https://docs.djangoproject.com/en/1.5/topics/forms Working with forms 关于此文章: ...
- nutch搏斗之一
nutch搏斗之一 问题描述: 在用nutch1.0做generate 包括5亿url的crawldb时,它默认按照64M分块,分成777个map task,在运行的后期出现 Could not fi ...
- A Game of Thrones(7) -Arya
Arya’s stitches were crooked again. She frowned down at them with dismay and glanced over to where h ...
- pthread_once()使用(某个时间在整个程序中仅执行一次,不确定是那个线程)
在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main函数中.但当你写一个库时,就不能在main里面初始化了,你可以用静态初始化,但使用一次初始化(pthread ...
- cocos2dx-lua牧场小游戏(一)
环境: cocos2dx-3.0rc2, xcode5.0 一.lua项目建立參考 http://blog.csdn.net/daydayup_chf/article/details/249641 ...
- uva10791 uva10780(分解质因数)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 动画(Animation) 它 (闪烁、左右摇摆、跷跷板等功效)
一侧到另一侧的影响: (这里显示的是并不那么顺利) 一.续播 (不知道取什么名字好,就是先播放动画A, 接着播放动画B) 有两种方式. 第一种.分别动画两个动画,A和B, 然后先播放动画A,设置A ...
- Python3.4 邮件(包含附件与中国)
import smtplib import os from email.mime.text import MIMEText from email.mime.multipart import MIMEM ...