怎么控制IE浏览器?

来源:百度知道 编辑:UC知道 时间:2024/06/18 03:34:00
想写一个桌面程序,用C#。
程序运行后,会用IE打开指定的网页,并自动登录网站,再根据需要进行一些操作。
关键是不知道怎么控制IE浏览器,请大家指点一下。

C#控制IE浏览器
引入 C:\WINDOWS\System32\mshtml.tlb、Interop.SHDocVw.dll

/// <summary>
/// 返回指定Url的IE窗口下的 IHTMLDocument2 对象。
/// </summary>
/// <returns>IHTMLDocument2</returns>
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