添加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交互的更多相关文章

  1. webbrowser 控件实现WinForm与WebForm交互

    WebBrowser 控件可以让你装载Windows Form 应用程序中的 Web 网页和其它采用浏览器的文件.可以使用webbrowser 控件将现有的web框架控制项加入至 Windows Fo ...

  2. C# 网络编程之网页自动登录 (一).使用WebBrower控件模仿登录

    最近学习C#网络编程中,想实现网页自动登录并提交GET/POST信息,再实现循环登录不断发送报文给服务器,服务器发送消息给客户端记录能登录的账户和密码,做到后面实现绕过验证码.动态抓取登录位置等,但由 ...

  3. Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题)

     Halcon的HWindowControl控件在WinForm程序中的使用介绍(重点解决图片缩放的问题) 2016-12-04 20:11 362人阅读 评论(4) 收藏 举报  分类: Halco ...

  4. C#常见控件与SQL Sever数据库交互

    C#常见控件与SQL Sever数据库交互 下拉框(ComboBox)与数据库绑定 首先,我们采用DataSet作为临时的数据库,这样会比较好 那么,我们先创建两个成员(对象) string sqlc ...

  5. 浏览器自动化的一些体会5 webBrowser控件之winform和webBrowser的交互

    从winform访问webBrowser,大致就是利用webBrowser提供的解析dom的方法以及用InvokeScript方法执行javascript.这个相对比较简单. 从webBrowser访 ...

  6. WPF控件--利用Winform库中的NotifyIcon实现托盘小程序

    WPF控件--NotifyIcon   运行界面如下所示:            图1                                             图2 代码很少,如下所示 ...

  7. 自己写的一个分页控件类(WinForm)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  8. jquery文件上传控件 Uploadify 可以和ajax交互

    http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html  原网址 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...

  9. Winform控件WebBrowser与JS脚本交互

    1)在c#中调用js函数 如果要传值,则可以定义object[]数组. 具体方法如下例子: 首先在js中定义被c#调用的方法: function Messageaa(message) {       ...

随机推荐

  1. Android开发 更改返回button的图标

    非常多的Android应用左上角都有返回button 在默认的情况下 ADT会默认给一个返回图标 而作为开发需求 非常多都要求定制一个新的图标 在Android的站点上 发现了2种能够更改的方法 1. ...

  2. sql优化-提防错误关联

    在写sql时,在多表关联时,有时候容易把关联关系写错.一般情况下,该问题比较容易发现,但如果sql较长时,光靠眼力就比较难发现了.今天写了一个脚本,碰到该问题了. 第一版本的脚本如下: select ...

  3. Linux Shell脚本入门--grep命令详解

    grep简介<摘自鸟哥,并加以整理.> grep (global search regular expression(RE) and print out the line,全面搜索正则表达 ...

  4. ftk学习记(waitbox篇)

    [声明:版权全部.欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 前面说到了脚本.那么就看看ftk中demo与script搭配的效果是什么样的? 上面的效果图就相 ...

  5. Note for video Machine Learning and Data Mining——Linear Model

    Here is the note for lecture three. the linear model Linear model is a basic and important model in ...

  6. 一个用js写的接口http调试程序

    公司有非常多手机app的项目.手机app又要常常訪问后台提交与查询数据. 所曾经端app与后台的开发与測试过程中接口调试是一个常常要做的工作. 而每当出现一个BUG,前端appproject师与后台p ...

  7. linux--shell script

    下面是最近学习shell script的一些知识点总结 ***博客园-邦邦酱好***   1.介绍shell是一个文字接口,让我们与系统沟通.shell script就是针对shell所写的脚本.它就 ...

  8. python学习笔记之五:抽象

    本文会介绍如何将语句组织成函数,还会详细介绍参数和作用域的概念,以及递归的概念及其在程序中的用途. 一. 创建函数 函数是可以调用,它执行某种行为并且返回一个值.用def语句即可定义一个函数:(并非所 ...

  9. linux查看CPU和内存信息

    一 先来看看ps命令: 1.查看当前某个时间点的进程:ps命令就是最基本同时也是非常强大的进程查看命令.使用该命令可以确定有哪些进程正在运行和运行的状态.进程是否结束.进程有没有僵死. 哪些进程占用了 ...

  10. zoj Reactor Cooling

    Reactor Cooling 无源汇上下界最大流问题. 1.流量平衡. 2.满足上下界 模板题. #include <iostream> #include <queue> # ...