首页 > 解决方案 > 如何从 iframe 访问 HTML

问题描述

我有一个像

<iframe tabindex="2" class="s-basicpanel s-panel-border" id="test1">
  <html>
   <body id ="test"></body>
  </html>
</iframe>

我必须在正文标签中添加内部文本。

我做了以下事情:

public void testData()
{
    mshtml.IHTMLWindow2 frame2 = IEDOMDocument.frames.item(ref 0);
    if (frame2 != null)
    {
        if(frame2.document.body != null)
        {
            mshtml.IHTMLElementCollection  allElements = (mshtml.IHTMLElementCollection)frame2.document.body.all;
            foreach(mshtml.IHTMLElement test in  allElements)
            {
                Log("Tag Name:"+test.tagName);
                if(test.tagName.ToLower() == "iframe")
                {
                    string iframeId=test.getAttribute("id").ToString();
                    if(iframeId.Equals("test1"))
                    {
                        Log("found elemnet***");
                        mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)test.children; // return me empty collection
                    }
                }
            }
        }
    }
}

当我尝试访问 iframe 中的内部集合时使用上面的代码,它为我提供了空集合。

经过大量调试后,我知道问题出在 iframe 中的数据上。 我需要有人来帮忙。

标签: c#html.net

解决方案


推荐阅读