ITPub博客

首页 > 应用开发 > IT综合 > c#控制IE浏览器

c#控制IE浏览器

原创 IT综合 作者:mrhaozi 时间:2010-03-18 21:58:34 0 删除 编辑

c# 控制IE浏览器

想写一个桌面程序,用C#。
程序运行后,会用IE打开指定的网页,并自动登录网站,再根据需要进行一些操作。


相关内容如下:

C#控制IE浏览器
引入 C:WINDOWSSystem32mshtml.tlb、Interop.SHDocVw.dll


///
/// 返回指定Url的IE窗口下的 IHTMLDocument2 对象。
///

/// IHTMLDocument2
public static IHTMLDocument2 GetIHTMLDocument2ByUrl(string url)
{
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
string filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if (filename.Equals(“iexplore“) && ie.LocationURL == url)
{
return ie.Document as IHTMLDocument2;
}
}
}

通过 GetIHTMLDocument2ByUrl 方法可以获取已打开的IE窗口中指写地址的窗口中的 IHTMLDocument2 对象。
利用这个对象,就可以进行相关操作。
1.填写表单
IHTMLDocument2 iHTMLDocument2 = GetIHTMLDocument2ByUrl(“http://www.163.com“);
IHTMLInputElement input = (IHTMLInputElement)iHTMLDocument2.all.item(“Username“, 0); // 获取指定名称的对象
input.value = “Xiao“; // 赋值


2.点击按钮
IHTMLDocument2 iHTMLDocument2 = GetIHTMLDocument2ByUrl(“http://www.163.com“);
HTMLDocumentClass obj = (HTMLDocumentClass)iHTMLDocument2;
IHTMLElement iHTMLElement = null;
IHTMLElementCollection c = obj.getElementsByTagName(“input“);
foreach (IHTMLElement e in c)
{
if (e.outerHTML.IndexOf(“登录“) != -1)
{
iHTMLElement = e;
break;
}
}
if (iHTMLElement != null)
{
iHTMLElement.click(); // 点击登录按钮
}

更多功能可以参考 IHTMLDocument2 对象
[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/16396910/viewspace-1032148/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 网页数据采集器
请登录后发表评论 登录
全部评论

注册时间:2009-11-28

  • 博文量
    125
  • 访问量
    333471