public class WebBrowserAssistant
{
System.Windows.Forms.WebBrowser wb;
public WebBrowserAssistant(System.Windows.Forms.WebBrowser wb) {
this.wb = wb;
}
public bool SetElementValue(string id,string value) {
this.wb.Document.GetElementById(id).SetAttribute("value", value);
return true;
} public object InvokeScript(string scriptName) {
return this.wb.Document.InvokeScript(scriptName);
}
public object InvokeScript(string scriptName,object[] ps)
{
return this.wb.Document.InvokeScript(scriptName, ps);
}
public void GetFocus(string id)
{
this.wb.Document.GetElementById(id).Focus();
}
public void GetBlur(string id)
{
this.wb.Document.GetElementById(id).RemoveFocus();
}
public string GetInputIDByName(string name)
{
return GetInputIDByName(name, name);
} public void ClickElement(string id) {
this.wb.Document.GetElementById(id).InvokeMember("click");
}
public string GetInputIDByName(string name,string id)
{
System.Windows.Forms.HtmlElementCollection es = this.wb.Document.GetElementsByTagName("input");
if (es != null && es.Count > ) {
System.Collections.IEnumerator ie = es.GetEnumerator();
System.Windows.Forms.HtmlElement e;
while (ie.MoveNext()) {
e = (System.Windows.Forms.HtmlElement)ie.Current;
if (e.Name.Equals(name))
{
if (string.IsNullOrEmpty(e.Id))
{
e.Id = id;
}
return e.Id;
}
}
}
return null;
} public List<string> GetEletemtByName(string name)
{
System.Windows.Forms.HtmlElementCollection es = this.wb.Document.GetElementsByTagName("input");
if (es != null && es.Count > )
{
List<string> result = new List<string>();
System.Collections.IEnumerator ie = es.GetEnumerator();
System.Windows.Forms.HtmlElement e;
while (ie.MoveNext())
{
e = (System.Windows.Forms.HtmlElement)ie.Current;
if (e.GetAttribute("type").Equals("checkbox") && e.Name.Equals(name) && e.GetAttribute("checked").Equals("True"))
{
result.Add(e.GetAttribute("value"));
}
}
return result;
}
return null;
}
public string ExeScript(string scriptBlock) {
return ExeScript(scriptBlock, "tempEletemtTagNameAndID");
}
public string ExeScript(string scriptBlock,string tempEletemtTagNameAndID) {
System.Windows.Forms.HtmlElement ele;
if (this.wb.Document.GetElementById(tempEletemtTagNameAndID) == null) {
ele = this.wb.Document.CreateElement(tempEletemtTagNameAndID);
ele.Id = tempEletemtTagNameAndID;
this.wb.Document.Body.AppendChild(ele);
}
ele = this.wb.Document.CreateElement("script");
ele.SetAttribute("type", "text/javascript"); ele.SetAttribute("text", string.Format("$j(\"#{0}\").val({1})", tempEletemtTagNameAndID, scriptBlock));
this.wb.Document.Body.AppendChild(ele); return this.wb.Document.GetElementById(tempEletemtTagNameAndID).GetAttribute("value");
} }

调用

MessageBox.Show(new Huawei.PortableComputer.Util.WebBrowserAssistant(this.wbTaskList).ExeScript("$j('.listContentTable td :checkbox:checked:first').parent().next().next().next().text()"));

【NET】WebBrowser执行脚本以及一般操作代码的更多相关文章

  1. WebBrowser执行脚本和调用外部方法

    控制WebBrowser实际上就是控制IE,最简单的方法就是执行javascript或vbscript,省去了接口的转换.如何执行脚本?以前我一直用mshtml中IHTMLWindow2接口的exec ...

  2. WebBrowser执行脚本

    ExecuteJavaScript(WebBrowser:TWebBrowser; Code: string):Variant;var //发送脚本Document:IHTMLDocument2;Wi ...

  3. winform中WebBrowser控件执行脚本

    在实际应用中我们可能需要WebBrowser控件主动执行我们需要的脚本,执行脚本现在有两种方法. 1.WebBrowser控件加载脚本,简单方便,适用于简短的脚本,无法执行复杂操作. webBrows ...

  4. winform下利用webBrowser执行javascript

    目前很多网站为了防止恶意提交表单信息,大多都采用了加密的方式对提交信息进行处理,加密处理后通过POST提交给服务器验证,这种操作一般都是用Javascipt进行加密,若是我们想要正确提交表单到网站,就 ...

  5. Tsung脚本中使用动态参数(一)---直接在脚本里编写Erlang代码

    杀死一个程序猿,只要改三次需求.同理,杀死一个接口自动化测试人员,只要改三次接口数据处理方式.我目前的状态,改了一次接口数据处理方式,有一种胸闷的感觉. 因为改需求,所以,要改脚本.T_T.所以,才有 ...

  6. Android手机上,利用bat脚本模拟用户操作

    ………… 那么你就可以来看看这篇帖子了. 言归正传 利用bat脚本模拟用户操作,需要用到两点: ①就是adb命令了,adb命令可以用来模拟用户在手机上的操作 ②bat语言,就是批处理语言,主要用来进行 ...

  7. JSch远程执行脚本

    JSch远程执行脚本 2017-02-24 在自动化测试的时候,需要远程操控服务器做一些操作,比如切日.起服务器.执行某些脚本.如何实现? 我们可以利用JSch,远程执行脚本.JSch是Java Se ...

  8. PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)

    源码地址:https://github.com/Tinywan/PHP_Experience 测试环境配置: 环境:Windows 7系统 .PHP7.0.Apache服务器 PHP框架:ThinkP ...

  9. ExtJS 4.1 TabPanel动态加载页面并执行脚本【转】

    ExtJS 4.1 TabPanel动态加载页面并执行脚本 按照官方示例,可以动态加载页面,可是脚本不执行,于是查SDK.google,发现scripts需要设置为true,于是设置该属性,整个代码如 ...

随机推荐

  1. UI 收集

    semantic http://www.semantic-ui.com.cn/modules/reveal.html sbadmin http://startbootstrap.com/templat ...

  2. MySQL 一般查询日志(General Query Log)

    与大多数关系型数据库,日志文件是MySQL数据库的一个重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志.慢查询日志,等等. 这些日志能够帮助我们定位mysqld ...

  3. 一步一步写算法(之prim算法 中)

    原文:一步一步写算法(之prim算法 中) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] C)编写最小生成树,涉及创建.挑选和添加过程 MI ...

  4. EasyMonkeyDevice vs MonkeyDevice&HierarchyViewer API Mapping Matrix

    1. 前言 本来这次文章的title是写成和前几篇类似的<EasyMonkeyDevice API实践全记录>,内容也打算把每个API的实践和建议给记录下来,但后来想了下觉得这样子并不是最 ...

  5. Erlang常用代码段

    十六进制字符串转为二进制 hex_to_bin(Bin) -> hex2bin(Bin). hex2bin(Bin) when is_binary(Bin) -> hex2bin(bina ...

  6. 有趣的游戏:Google XSS Game

    Google最近出了一XSS游戏: https://xss-game.appspot.com/ 我这个菜鸟看提示,花了两三个小时才全过了.. 这个游戏的规则是仅仅要在攻击网页上弹出alert窗体就能够 ...

  7. Redis3

    Redis到底该如何利用 上两篇受益匪浅,秉着趁热打铁,不挖到最深不罢休的精神,我决定追加这篇.上一篇里最后我有提到实现分级缓存管理应该是个可行的方案,因此今天特别实践了一下.不过缓存分级之后也发现了 ...

  8. ASP.NET WebForm路由模拟

    一.ASP.NET MVC 路由(一)--- ASP.NET WebForm路由模拟 2014-11-08 11:49 by 郝喜路, 232 阅读, 0 评论, 收藏, 编辑 ASP.NET Web ...

  9. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  10. Linux Shell 示例精解(第七章 gawk编程)转载

    第七章 gawk功能:gawk编程 7.1.1 数字和字符串常量     初始化和类型强制  在awk中,变量不需要定义就可以直接使用,使用一个变量就是对变量的定义.变量的类型可以试数字.字符串,或者 ...