【问题标题】:Does WebBrowser.DocumentText as well contains all frame documents text?WebBrowser.DocumentText 是否也包含所有框架文档文本?
【发布时间】:2012-03-22 06:36:03
【问题描述】:

我不确定WebBrowser.DocumentText 是否仅包含顶级文档源或还包含框架文档文本。无法从 MSDN 页面找到。

【问题讨论】:

    标签: c# .net browser


    【解决方案1】:

    不,它没有。我已经尝试了下一个:

    文档文本:

    File.WriteAllText(@"C:\doc.txt", webBrowser1.DocumentText, Encoding.UTF8);
    

    GetElementsByTagName("HTML")

    HtmlElement elem;
    if (webBrowser1.Document != null)
    {
        HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("HTML");
        if (elems.Count == 1)
        {
            elem = elems[0];
            string pageSource = elem.OuterHtml;
            File.WriteAllText(@"C:\doc.txt", pageSource, Encoding.UTF8);
        }
    }
    

    IOleCommandTarget

    public void ShowSource()
    {
        IOleCommandTarget cmdt = null;
        object o = null;
        object oIE = null;
        try {
                cmdt = (IOleCommandTarget)this.Document.DomDocument;
                cmdt.Exec(cmdGUID, oCommands.ViewSource, 1, o, o);
        } catch (Exception ex) {
                throw new Exception(ex.Message.ToString(), ex.InnerException);
        } finally {
                cmdt = null;
        }
    }
    

    唯一的方法是遍历所有框架文档。

    更新如果 iframe 有不同的 url 你会得到 UnauthorizedAccessException when trying to retrieve iframe document

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 2014-12-27
      • 2013-10-04
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多