zoukankan      html  css  js  c++  java
  • c# 控制IE浏览器

    想写一个桌面程序,用C#。
    程序运行后,会用IE打开指定的网页,并自动登录网站,再根据需要进行一些操作。
    关键是不知道怎么控制IE浏览器,请大家指点一下。

    相关内容如下:

    C#控制IE浏览器
    引入 C:WINDOWSSystem32mshtml.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 对象。

  • 相关阅读:
    Token ,Cookie和Session的区别
    极致Web性能 —— SPA性能指南
    关于前端数据&逻辑的思考
    移动端Retina屏boder 1px显示为2px或3px的解决方法
    Java连载8-基本数据类型2
    HTML连载25-通配符选择器&选择器综合练习
    Python连载25-函数tell&write&writeline$&持久化
    Python连载24-函数list&read&seek
    Java连载7-变量&数据类型
    HTML连载24-属性选择器(下)
  • 原文地址:https://www.cnblogs.com/soundcode/p/10772982.html
Copyright © 2011-2022 走看看