C#程序 给IE网页IFRAME控件中所嵌入网页的元素赋值

时间:2024-01-09 16:07:26

//引用COM组件
//Microsoft HTML Object Library
//Microsoft Internet Controls

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
string filename;
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if (filename.Equals("iexplore"))
{
SetText(string.Format("Web Site  : {0}", ie.LocationURL));
mshtml.IHTMLDocument2 htmlDoc = ie.Document as mshtml.IHTMLDocument2;
SetText(string.Format("  Document Snippet: {0}",
((htmlDoc != null) ? htmlDoc.body.outerHTML
: "***Failed***")));
SetText(string.Format("{0}{0}", Environment.NewLine)); mshtml.HTMLInputElementClass input;
mshtml.HTMLIFrameClass iframe;
mshtml.IHTMLDocument2 target;
//获取父页面框架集合.
mshtml.IHTMLFramesCollection2 frames = htmlDoc.frames;
for (int i = ; i < frames.length; i++)
{
object index = i;
mshtml.IHTMLWindow2 frame = frames.item(ref index) as mshtml.IHTMLWindow2; mshtml.IHTMLDocument2 frameDoc = frame.document as mshtml.IHTMLDocument2;
//现在你可以通过frameDoc获取子页面控件. if (frame.location.href.Contains("zzs_fp.html"))
{
target = frameDoc; foreach (mshtml.IHTMLElement ieElement in target.all)
{
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = ((mshtml.HTMLInputElementClass)ieElement); SetText("input.name:" + input.name);
SetText(string.Format("{0}{0}", Environment.NewLine)); if (input.name == "ghdwmc")
{
input.value = "****";
}
else if (input.name == "ghdwsh")
{
input.value = "12300000747150428A";
}
else if (input.name == "ghdwdzdh")
{
input.value = "北京市 010-512011";
}
else if (input.name == "ghdwyhzh")
{
input.value = "北京银行 132465798798";
}
}
}
}
}
}
}