public static List <InternetExplorer> ieInstances()
        {
            var          ieInstances  = new List <InternetExplorer>();
            ShellWindows shellWindows = new ShellWindowsClass();

            for (int i = 0; i < shellWindows.Count; i++)
            {
                if (shellWindows.Item(i) is InternetExplorer)
                {
                    var instance = (InternetExplorer)shellWindows.Item(i);
                    //make sure it is a browser instance (since we don't want the Windows Explorer cases)
                    if (instance.FullName.contains("IEXPLORE.EXE"))
                    {
                        ieInstances.Add(instance);
                    }
                }
            }
            return(ieInstances);
        }
Exemple #2
0
        private void button3_Click(object sender, EventArgs e)
        {
            //查找打开的窗口句柄
            int iehwnd = FindWindowA("IEFrame", "百度一下,你就知道 - Internet Explorer");

            //初始化所有IE窗口
            IShellWindows sw = new ShellWindowsClass();

            //轮询所有IE窗口
            for (int i = sw.Count - 1; i >= 0; i--)
            {
                //得到每一个IE的 IWebBrowser2 对象
                IWebBrowser2 iwb2 = sw.Item(i) as IWebBrowser2;
                //比对 得到的 句柄是否符合查找的窗口句柄
                if (iwb2.HWND == iehwnd)
                {
                    //查找成功 进行赋值
                    document = (HTMLDocumentClass)iwb2.Document;
                    //对网页进行操作
                    document.getElementById("kw").setAttribute("value", "1111");
                    //document.getElementById("kw").onclick();
                }
            }
            //IntPtr parent = FindWindow("IEFrame", null);
            //int child = FindWindowEx(parent.ToInt32(), 0, "Frame Tab", null);
            //child = FindWindowEx(child, 0, "TabWindowClass", null);
            //child = FindWindowEx(child, 0, "Shell DocObject View", null);
            //child = FindWindowEx(child, 0, "Internet Explorer_Server", null);

            ////var oBrowser = DiagnosticsGlobalScope.browser;
            //IHTMLDocument2 doc =(IHTMLDocument2)GetDocument(child);
            //MessageBox.Show(doc.title);
            //IHTMLElement2 elm=(IHTMLElement2)doc.all.item("wd",0);
            //MessageBox.Show(elm.ToString());
            //FramesCollection frames= doc.frames;
            ////if (frames.length > 0)
            ////{
            ////    for(int i = 0; i < frames.length; i++)
            ////    {
            ////        IHTMLWindow2 window=frames.item(i);
            ////    }
            ////}
            //IHTMLElementCollection coll = doc.forms;
            //if (coll.length > 0)
            //{
            //    for (int i = 0; i < coll.length; i++)
            //    {
            //        IHTMLElement window = coll.item(i);
            //        string name=window.getAttribute("name");
            //        if (name == "wd")
            //        {
            //            window.setAttribute("value", "1");
            //        }
            //    }
            //}
            //IHTMLDOMChildrenCollection collect = (IHTMLDOMChildrenCollection)doc.childNodes;

            //foreach (IHTMLDOMNode node in collect)
            //{
            //    //因为关闭节点也会有(比如</a>,但是这样的节点会被定义为HTMLUnknownElementClass)
            //    //所以要判断这个节点是不是未知节点不是才处理
            //    if (!(node is IHTMLUnknownElement))
            //    {

            //        //获取属性集合
            //        IHTMLAttributeCollection attrs = (IHTMLAttributeCollection)node.attributes;
            //        foreach (IHTMLDOMAttribute attr in attrs)
            //        {

            //            //只有specified=true的属性才是你要的
            //            if (attr.specified)
            //            {
            //                Console.Write(attr.nodeValue);
            //            }
            //        }
            //    }
            //}
        }