栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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代码,有一些nr
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;
        }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910798.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号