首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从框架访问HTML?

如何从框架访问HTML?
EN

Stack Overflow用户
提问于 2018-08-15 05:10:43
回答 1查看 0关注 0票数 0

我的结构像

代码语言:txt
复制
<iframe tabindex="2" class="s-basicpanel s-panel-border" id="test1">
  <html>
   <body id ="test"></body>
  </html>
</iframe>

我接下来做了:

代码语言:txt
复制
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
                    }
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-08-15 14:14:47

你有:

代码语言:txt
复制
foreach(mshtml.IHTMLElement test in  allElements)
{
    Log("Tag Name:"+test.tagName);
    if(test.tagName.ToLower() == "iframe")
    {
        if(iframeId.Equals("test1"))
        {
            Log("found elemnet***");

            //you're getting the children of val here, but should be getting children of test
            mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)val.children;   
        }
    }
}

应该是mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)test.children

一种更简单、不那么复杂的方法是搜索整个文档以查找元素的id,然后进行所需的更改:

代码语言:txt
复制
if (frame2.document != null)
{
    var element = frame2.document.getElementById("test");

    if (element != null)
        element.innerText = "some text";
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档