网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

IE浏览器控制,C#利用JS操作IE

时间:2024-10-14 02:31:12

1、先打开一个IE浏览器窗口,新建一个C#项目右击项目目录的【引用】,添加引用。在.NET选项卡添加名字为【Microsoft.mshtml】的引用

IE浏览器控制,C#利用JS操作IE

3、写一个jsRun的方法,方便频繁的向浏览器的指定网页中动态执行JS脚本其中Brower传递我们要执行脚本的对象,而str是我们想要执行的js: private void jsRun(InternetExplorer Browser,String str) { HTMLDocument doc = Browser.Document as HTMLDocument; HTMLScriptElement script = (HTMLScriptElement)doc.createElement("script"); script.text = str; HTMLBody body = doc.body as HTMLBody; body.appendChild((IHTMLDOMNode)script); }

IE浏览器控制,C#利用JS操作IE

5、完整代码如下:#region Using directivesusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using mshtml;using SHDocVw;#endregionnamespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { ShellWindows shellWindows = new ShellWindowsClass(); foreach (InternetExplorer Browser in shellWindows) { if (Browser.Document is HTMLDocument) { jsRun(Browser, "alert(889966);"); jsRun(Browser, "alert(00000);"); } } } private void jsRun(InternetExplorer Browser,String str) { HTMLDocument doc = Browser.Document as HTMLDocument; HTMLScriptElement script = (HTMLScriptElement)doc.createElement("script"); script.text = str; HTMLBody body = doc.body as HTMLBody; body.appendChild((IHTMLDOMNode)script); } }}

IE浏览器控制,C#利用JS操作IEIE浏览器控制,C#利用JS操作IE
© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com