我的结构像
<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
}
}
}
}
}
}
发布于 2018-08-15 14:14:47
你有:
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,然后进行所需的更改:
if (frame2.document != null)
{
var element = frame2.document.getElementById("test");
if (element != null)
element.innerText = "some text";
}
https://stackoverflow.com/questions/-100005991
复制相似问题