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. 饼干怪兽和APT攻击

    APT攻击就像一个孩子,你通过各种方式窃取他们的大脑要拿出饼干,为了防止恶意攻击,过失作为母亲未能发现和防止饼干盗窃贼如.于她仅仅监视厨房椅子.衣柜门或烤箱门的开启.建立起有效防御目标攻击与APT攻击 ...

  2. POJ 3176-Cow Bowling(DP||记忆化搜索)

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14210   Accepted: 9432 Desc ...

  3. 从WebBrowser中取得Cookie 和 WebClient设置cookie!

    原文:从WebBrowser中取得Cookie 和 WebClient设置cookie! 从WebBrowser中取得Cookie 的代码 CookieContainer myCookieContai ...

  4. MVC验证09-使用MVC的Ajax.BeginForm方法实现异步验证

    原文:MVC验证09-使用MVC的Ajax.BeginForm方法实现异步验证 MVC中,关于往后台提交的方法有: 1.Html.BeginForm():同步 2.Ajax.BeginForm():异 ...

  5. 写给初学前端工程师的一封信 (转于Kejun)

    大家好: 应波波的邀请写一写我对这个话题的想法.从去年开始不少朋友让我帮忙介绍前端工程师,绝大部分忙都没帮上,原因是真找不到人.我当时是这么跟他们分析的:过去的客户端以browser为主,所以HTML ...

  6. mongodb操作:利用javaScript封装db.collection.find()后可调用函数源码解读

    { "_mongo" : connection to YOURIP:27017{ SSL: { sslSupport: false, sslPEMKeyFile: "&q ...

  7. 谢绝艳照门 - 手把手教你把当今很hit的家庭监控IP Camera变得网络安全起来

    IP Camerars现在已经越来越便宜了,很多人都可以买得起,并且大家也乐意去购买,因为它们的确是用来监控你在高房价的中国购买的爱巢的非常便利的设备.当然,配套的监控应用也层出不穷,从通用的家庭安全 ...

  8. Python 图论工具

    networkx: 一个用Python语言开发的图论与复杂网络建模工具, 内置了经常使用的图与复杂网络分析算法, 能够方便的进行复杂网络数据分析.仿真建模等工作. 依赖工具: numpy  pypar ...

  9. 检测浏览器版本类型的JavaScript代码,终极版

    下面的JavaScript代码,不仅可以判断PC端浏览器类型,还可以判断安卓.iOS.其他智能手机.平板电脑或游戏系统. 说废话貌似不是我的风格哈,直接上代码吧: var client = funct ...

  10. LeetCode之Maximum Product Subarray

    1.(原文)问题描述 Find the contiguous subarray within an array (containing at least one number) which has t ...