WebBrowser和内嵌网页的交互
来源:互联网 发布:冰川网络上市定位 编辑:程序博客网 时间:2024/05/16 06:23
通过WebBrowser可以和内嵌其中的网页进行交互。现在分享大部分核心代码如下:
#region Html event and set value methods private void SetValueById(string id, string value) { if (tabBrowser.CurrentWebBrowser.Document != null) { HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id); if (he != null) { he.SetAttribute("value", value); } } } private void SetInputValueByName(string name, string value) { HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName("input"); if (hc != null) { foreach (HtmlElement he in hc) { if (he.Name == name) { he.SetAttribute("value", value); break; } } } } private void SetSelectedIndexById(string id, string value) { if (tabBrowser.CurrentWebBrowser.Document != null) { HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id); if (he != null) { he.SetAttribute("selectedIndex", value); } } } private void SetSelectedIndexByName(string name, string value) { if (tabBrowser.CurrentWebBrowser.Document != null) { HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName("select"); if (hc != null) { foreach (HtmlElement he in hc) { if(he.Name == name) he.SetAttribute("selectedIndex", value); } } } } private void SetCheckBoxById(string id, string value) { if (tabBrowser.CurrentWebBrowser.Document != null) { HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id); if (he != null) { he.SetAttribute("checked", value); } } } private void FireEvent(string id, string eventName, params object[] args) { if (tabBrowser.CurrentWebBrowser.Document != null) { HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id); if (he != null) { he.InvokeMember(eventName, args); } } } private void FireChildClickEvent(string parentId, int childIndex) { HtmlElement heDiv = tabBrowser.CurrentWebBrowser.Document.GetElementById(parentId); if (heDiv != null && heDiv.Children.Count > childIndex) { heDiv.Children[childIndex].InvokeMember("click", null); } } private void FireScriptOnPage(string functionname) { if (tabBrowser.CurrentWebBrowser.Document != null) { tabBrowser.CurrentWebBrowser.Document.InvokeScript(functionname); } } private void FireScriptOnPage(string functionname, object[] args) { if (tabBrowser.CurrentWebBrowser.Document != null) { tabBrowser.CurrentWebBrowser.Document.InvokeScript(functionname, args); } } private void FireClickEvent(string id) { FireEvent(id, "click"); } private void FocusOnOption(string selectname, string optionvalue) { HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName("select"); if (hc != null) { foreach (HtmlElement he in hc) { if (he.Name == selectname) { foreach (HtmlElement c in he.Children) { if (c.GetAttribute("value") == optionvalue) { c.SetAttribute("selected", "true"); return; } } } } } } private void FireClickEventOnInput(string name) { HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName("input"); if (hc != null) { foreach (HtmlElement he in hc) { if (he.Name == name) { he.InvokeMember("click"); break; } } } } private HtmlElement GetElementByTagName(string tagName, string name) { HtmlElementCollection hc = tabBrowser.CurrentWebBrowser.Document.GetElementsByTagName(tagName); if (hc != null && hc.Count > 0) { foreach (HtmlElement he in hc) { if (he.Name == name) return he; } } return null; } private void SendKeysToElement(string id, string keys) { HtmlElement he = tabBrowser.CurrentWebBrowser.Document.GetElementById(id); if (he != null) { he.Focus(); SendKeys.Send(keys); } } #endregion
方法名还是很明了的,有很多是要通过ElementId来操作的,ID可以通过IE Developer Tools或者Firefox的插件获得,手段有很多。
SendKeysToElement可以把键盘按键发送给指定控件,有些按键是特殊按键,可以参见MSDN:
SendKeysToElement("rsid_select_drop_down_input", “Sample Here”);
FireScriptOnPage用来调用Javascript的方法,有接受参数的重载方法。
加上这篇介绍的控件鼠标位置,可以适用大部分场景。
- WebBrowser和内嵌网页的交互
- Delphi WebBrowser与网页交互
- C# WinForm webBrowser 内嵌网页的按钮的OnClientClick事件的return false 在webBrowser中绑定onclick事件后 失效 的变通解决办法
- Winform 应用程序内嵌Webbrowser的若干问题
- 网页通过External接口与WebBrowser交互
- WPF 与网页交互(Frame,WebBrowser)
- 用WebBrowser实现HTML界面的应用和交互
- 用WebBrowser实现HTML界面的应用和交互
- 基于IE内核的浏览器:WebBrowser上的网页与Winform本身交互[Demo下载]
- 微信小程序与内嵌网页交互实现支付功能
- unity3d和网页数据交互的基本原理
- UIWebView和网页交互的研究初探
- unity3d和网页数据交互的基本原理
- Unity3D和网页数据交互的基本原理
- unity3d和网页数据交互的基本原理
- Unity3D和网页数据交互的基本原理
- Unity3D和网页数据交互的基本原理
- UIWebView和网页交互的研究初探
- jar下载的页面
- 教你如何精通Struts:Tiles框架
- Spring 配置问题还是代码问题?Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
- 从故事中感悟(四):司机考试
- Iframe 嵌入百度
- WebBrowser和内嵌网页的交互
- 遗憾也是一种美丽
- svg test web
- [改]HTTP调试工具:Fiddler 介绍二
- ASP.Net2.0小技巧
- 我的hotmail
- J2EE学习中一些值得研究的开源项目
- acm2009宁波地区赛排名
- cygwin关键技术:signal机制