c# 操作带frame的网页

来源:互联网 发布:tourex 源码 编辑:程序博客网 时间:2024/05/16 06:39
cs文件

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using mshtml;

namespace 学习项目
{
    public partial class Form1
    {
        public static IHTMLDocument2 GetDocumentFromWindow(IHTMLWindow2 htmlWindow)
        {
            if (htmlWindow == null) { return null; }
            try { IHTMLDocument2 doc = htmlWindow.document; return doc; }
            catch (COMException comEx)
            {
                if (comEx.ErrorCode != E_ACCESSDENIED) { return null; }
            }
            catch (System.UnauthorizedAccessException) { }
            catch
            {
                return null;
            }
            try
            {
                IServiceProvider sp = (IServiceProvider)htmlWindow;
                Object brws = null; sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out brws);
                SHDocVw.IWebBrowser2 browser = (SHDocVw.IWebBrowser2)(brws); return (IHTMLDocument2)browser.Document;
            }
            catch
            {
            }
            return null;
        }

        private const int E_ACCESSDENIED = unchecked((int)0x80070005L);
        private static Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
        private static Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");

        [ComImport(), ComVisible(true), Guid("6D5140C1-7436-11CE-8034-00AA006009FA"),
        InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IServiceProvider
        {
            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
        }

        public IHTMLElement getelement(IHTMLElementCollection a,string b,string c) //通过属性找到元素
        {
            IHTMLElement tel=null ;
            for (int tmp = 0; tmp < a.length; tmp++)
            {
                tel = a.item(tmp);
                if(tel.getAttribute (b)==c)
                {
                    return tel;
                }
            }
            return null;
        }

    }

}



使用:
private void 填写表单_Click(object sender, EventArgs e)
        {
            int temp = int.Parse(textBox5.Text);
            IHTMLDocument2 doc2 = GetDocumentFromWindow((IHTMLWindow2)this.webBrowser1.Document.Window.DomWindow);
            IHTMLFramesCollection2 fr2 = doc2.frames;
            IHTMLWindow2 win2 = fr2.item(1);
            IHTMLDocument2 doc3 = GetDocumentFromWindow(win2);
            IHTMLFramesCollection2 fr3 = doc3.frames;
            MessageBox.Show("FRAME有几个:" + fr3.length.ToString()); 
            //接下来分析iframe里面的源码
            IHTMLWindow2 win3 = fr3.item(temp);  
            IHTMLDocument2 doc4 = GetDocumentFromWindow(win3);
            IHTMLElementCollection el3 = doc4.all;
            IHTMLElement el2 = el3.item(1);
            MessageBox.Show("框架源码是:" + "\n" + el2.outerHTML);
            textBox2.Text = el2.outerHTML;
        }
原创粉丝点击