c#.net中获得webBrowser控件中嵌入的html页面的html代码

5 篇文章 0 订阅
5 篇文章 0 订阅
IHTMLDocument2 vDocument = webBrowser.Document.DomDocument as IHTMLDocument2;
IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow;
// 注入function方法
// vWindow.execScript("ShowMessage(67890);", "JavaScript"); // 执行脚本
// vWindow.execScript("setValue(999);", "JavaScript"); // 执行脚本
// // 获取脚本返回的值
//string temp =  vWindow.execScript("getValue();", "JavaScript"); 

// 获取控件的HtmlDocument,通过这个获取页面的一些值
HtmlDocument document = this.webBrowser.Document;

// 好像是和vWindow.execScript("getValue();", "JavaScript"); 相似
String s = (String)document.InvokeScript("getValue");
// 获取页面html代码,有一些\n\r
string strHtml = document.Body.InnerHtml;

☆下面是在网上找的,可能也好用,没有尝试

获取body内的html代码
            //string html = document.body.innerHTML;
            暂时没搞明白应该怎么获取所有的html代码

            根据ID获取元素
            根据id获得input
            //IHTMLElement inputSearch = (IHTMLElement)doc.all.item("kw");
            为input设置value属性
            //inputSearch.setAttribute("value", "设置搜索内容");
            获得搜索按钮
            //IHTMLElement submitSearch = (IHTMLElement)doc.all.item("su");
            点击按钮
            //submitSearch.click();
            获取窗体
            //mshtml.IHTMLWindow2 window = (mshtml.IHTMLWindow2)doc.parentWindow;

还有一些别的,没整理。


        private void SetWeb()
        {

            C#调用窗口
            HTMLDocument doc = this.webBrowser1.Document as HTMLDocument;
            //HtmlDocument main_formDoc = webBrowser1.Document;
            //IHTMLDocument2 main_mshtmlIHTMLDoc = (IHTMLDocument2)main_formDoc.DomDocument;
            //Object frame_index = 0;
            //IHTMLWindow2 target_mshtmlIHTMLWindow = (IHTMLWindow2)main_mshtmlIHTMLDoc.frames.item(ref frame_index);
            //IHTMLDocument2 target_mshtmlIHTMLDoc = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(target_mshtmlIHTMLWindow);
            //HTMLDocument doc = target_mshtmlIHTMLDoc as HTMLDocument;


            //mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;
            //win.execScript("changeRegImg()", "javascript");//使用JS


            IHTMLDocument2 vDocument = webBrowser.Document.DomDocument as IHTMLDocument2;
            IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow;
            Type vWindowType = vWindow.GetType();
            object testText = vWindowType.InvokeMember("testText",
                BindingFlags.GetProperty, null, vWindow, new object[] { }); // 读取
            Console.WriteLine(testText);
            vWindowType.InvokeMember("testText",
                BindingFlags.SetProperty, null, vWindow, new object[] { "Zswang 路过" }); // 设置
            vWindowType.InvokeMember("ShowMessage",
                BindingFlags.InvokeMethod, null, vWindow, new object[] { 12345 }); // 执行方法

            //获取body内的html代码
            //IHTMLElement inputSearch = (IHTMLElement)doc.
            //inputSearch.setAttribute("value", "设置搜索内容");
            //htmlText = webBrowser1.Document.Body.InnerHtml;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值