CefSharp获取页面Html

一、定义获取html的js方法

StringBuilder sb = new StringBuilder();
 sb.AppendLine("function tempFunction() {");
 //sb.AppendLine(" return document.body.innerHTML; "); 
 sb.AppendLine(" return document.getElementsByTagName('html')[0].innerHTML; ");
 sb.AppendLine("}");
 sb.AppendLine("tempFunction();");

 

二、循环页面iframe标签并执行上面定义的js方法获取html

 

for (int i = 0; i < chromeBrowser.GetBrowser().GetFrameNames().Count; i++)
 {
     var task1 = chromeBrowser.GetBrowser().GetFrame(chromeBrowser.GetBrowser().GetFrameNames()[i]).EvaluateScriptAsync(sb.ToString());
     task1.ContinueWith(t =>
     {
         if (!t.IsFaulted)
         {
             var response = t.Result;
             if (response.Success == true)
             {
                 if (response.Result != null)
                 {
                     string resultStr = response.Result.ToString();

                     if (resultStr.Contains(">型号 :"))  //获取打印机型号
                     {
                         Regex regex = new Regex(@">型号 : ([^>]*)</");
                         MatchCollection matches = regex.Matches(resultStr);
                         if (matches.Count > 0)
                         {
                             printType = matches[0].Groups[1].Value;
                         }
                     }
                     
                 }
             }
         }
     });
 }
           
 
View Code
posted @ 2024-08-26 14:20  ziff123  阅读(32)  评论(0编辑  收藏  举报