c# 控制IE浏览器

想写一个桌面程序,用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://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 对象。

转载于:https://www.cnblogs.com/soundcode/p/10772982.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 System.Diagnostics.Process 类来检查系统中是否有指定的进程在运行。首先,你需要获取到指定浏览器的进程名称。例如,IE浏览器的进程名称为 iexplore.exe。然后,你可以使用 Process.GetProcessesByName 方法来获取指定名称的所有进程,然后遍历这些进程,检查它们的主窗口句柄是否包含指定的 URL。 以下是一个示例代码,用于检查 IE 浏览器是否已经打开了指定的 URL: ```csharp using System.Diagnostics; public static bool IsUrlOpened(string url) { string processName = "iexplore"; // IE浏览器的进程名称 Process[] processes = Process.GetProcessesByName(processName); foreach (Process process in processes) { IntPtr handle = process.MainWindowHandle; if (handle != IntPtr.Zero) { string windowUrl = GetUrlFromWindowHandle(handle); if (windowUrl == url) { return true; } } } return false; } private static string GetUrlFromWindowHandle(IntPtr handle) { const int maxUrlLength = 2048; StringBuilder sb = new StringBuilder(maxUrlLength); int length = NativeMethods.GetWindowText(handle, sb, maxUrlLength); if (length > 0) { string windowTitle = sb.ToString(); if (windowTitle.StartsWith("about:") || windowTitle.StartsWith("http")) { return windowTitle; } } return string.Empty; } internal static class NativeMethods { [DllImport("user32.dll", CharSet = CharSet.Auto)] internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); } ``` 上面的代码中,我们使用了 `GetProcessesByName` 方法来获取所有名为 iexplore 的进程。然后,我们遍历这些进程的主窗口句柄,通过 `GetWindowText` 方法获取窗口标题,从而判断窗口是否包含指定的 URL。 请注意,这种方法有一些限制。首先,它仅适用于 IE 浏览器,如果你想检查其他浏览器是否打开了指定的 URL,你需要修改进程名称。此外,这种方法还可能会误判,因为一个浏览器窗口可以同时包含多个标签页,每个标签页可能包含不同的 URL,我们只能获取窗口的标题,无法精确判断哪个标签页包含指定的 URL。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值