【问题标题】:Read iFrame contents in a WPF browser window c#在 WPF 浏览器窗口 c# 中读取 iFrame 内容
【发布时间】:2015-06-10 06:53:47
【问题描述】:

我有一个带有 iFrame 的 HTML 文档,我正在将其加载到网络浏览器窗口中。我的目标是加载 iFrame 文档后,捕获文档内容并搜索特定行。

到目前为止我无法做到,有什么建议吗?

HTML 代码

<html>
  <body>
    <iframe id="monitor" src="http://monitor.baseline.com"></iframe>
  </body>
</html>

【问题讨论】:

    标签: c# html wpf iframe webbrowser-control


    【解决方案1】:

    您可以在 iFrame 的 OnLoad 事件中添加 JavaScript 函数:

    document.getElementById('monitor').onload = function() { }

    然后按照these solutions 解析您的文档。但请注意跨站点脚本限制。如果你的 iFrame 的 src 是一个完全不同的域,它可能不会工作。

    【讨论】:

    • 我想在 iframe 加载后阅读内容。尝试以下类似但似乎不起作用.. var Frame = document.getElementById('monitor');警报(Frame.interHTML);警报(Frame.outerHTML); window.external.iFrameCheck(Frame.outerHTML);
    • 我认为您不能将innerHTML 用于iFrame,因为它不是内部html,而是src 的内容。检查我发布的链接,有一些解决方案如何访问 iFrame 内容。
    【解决方案2】:

    我通过以下方式得到了它

    var javascript = @"
    
                            var Frame = document.getElementById('monitor');
                            window.external.iFrameCheck(Frame.contentWindow.document);
                            ";
    
                var doc = (IHTMLDocument2)webBrowser1.Document;
    
                //Once we have the document, execute our JavaScript in it
                doc.parentWindow.execScript(javascript);
    

    这将调用下面的 c# 代码

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
            [ComVisible(true)]
            public class ComVisibleObjectForScripting
            {
                public void iFrameCheck(HTMLDocument FrameDoc)
                {
                    //Do what you want here with your FrameDoc
                    // the innerHTML will give you the content of the iframe
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2014-05-12
      • 1970-01-01
      • 2011-06-13
      相关资源
      最近更新 更多