点击iframe内的按钮

本文关键字:按钮 iframe 点击 | 更新日期: 2023-09-27 17:54:44

我正在为一些非常老的应用程序创建一个DCOM扩展。主应用程序内部有IE控件。

使用c#,我能够得到处理程序的IE控件,如下所示:

Process pr = Process.GetCurrentProcess();
var title = pr.MainWindowTitle;
if(title.IndexOf("My old application -")<0)
{
    MessageBox.Show("No such window");
    return;
}
var wlwWindow = pr.MainWindowHandle;
var ieWindow = PI.FindWindowRecursive(wlwWindow, "Internet Explorer_Server", null);
if (ieWindow == IntPtr.Zero)
{
    MessageBox.Show("Unable to locate IE window.");
    return;
}
var myDocument= PI.GetIEDocumentFromWindowHandle(ieWindow);
MessageBox.Show(myDocument.url);

下面是我的辅助方法:

    public static IntPtr FindWindowRecursive(IntPtr parent, string windowClass, string windowCaption)
    {
        var found = FindWindowEx(parent, IntPtr.Zero, windowClass, windowCaption);
        if (found != IntPtr.Zero)
        {
            return found;
        }
        var child = FindWindowEx(parent, IntPtr.Zero, null, null);
        while (child != IntPtr.Zero)
        {
            found = FindWindowRecursive(child, windowClass, windowCaption);
            if (found != IntPtr.Zero)
            {
                return found;
            }
            child = GetNextWindow(child, 2);
        }
        return IntPtr.Zero;
    }
    public static IHTMLDocument2 GetIEDocumentFromWindowHandle(IntPtr hWnd)
    {
        IHTMLDocument2 htmlDocument = null;
        if (hWnd != IntPtr.Zero)
        {
            uint lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");
            UIntPtr lResult;
            SendMessageTimeout(hWnd, lMsg, UIntPtr.Zero, UIntPtr.Zero,
                SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lResult);
            if (lResult != UIntPtr.Zero)
            {
                htmlDocument = ObjectFromLresult(lResult, typeof(IHTMLDocument).GUID, IntPtr.Zero) as IHTMLDocument2;
                if (htmlDocument == null)
                {
                    throw new COMException("Unable to cast to an object of type IHTMLDocument");
                }
            }
        }
        return htmlDocument;
    }

这部分工作得很好,我能够从temp:

获得html页面的内容。
<body id="state" class="saveFavorite" onload="load()" onunload="RUIMaster.Destroy()" style="margin: 0px; padding: 0px; overflow: hidden;">
<p style="display: none;">
<object classid="clsid:22FD36F1-A133-11d4-A0E4-005056E2D8AA" height="0" width="0" id="RUIMaster">
</object>
</object>
<form>
<input id="id" type="hidden" value="" />
</form>
</p>
<iframe name="AAMain" style="margin: 0px; border: 0px; padding: 0px;" id="contents" src="rdaui_frame.htm" width="100%" height="100%" frameborder="0" />
</body>

我必须访问名为AAMain的iframe。
该iframe文档的内容如下所示:

<frameset cols="135,*" border="0" frameborder="no" framespacing="0">
    <frame name="Menu" scrolling="no" noresize marginwidth="0" marginheight="0" src="blank.htm">
    <frameset rows="310,*,31">
        <frame name="Top" noresize marginwidth="0" marginheight="0" src="blank.htm">
        <frame name="Centre" noresize marginwidth="0" marginheight="0" src="blank.htm">
        <frame name="Bottom" noresize marginwidth="0" marginheight="0" src="blank.htm">
    </frameset>
</frameset>
所以基本上我必须访问frame name center inside iframe named AAMain

myDocument -> iframe(AAMain) -> frame(Centre) ->然后点击image

我试图迭代myDocument的帧,但我得到无效的强制转换异常。

try
{
    FramesCollection frames = myDocument.frames;
    object index = 0;
    IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref index);
    var xx = (HTMLDocument)frame.document;
    MessageBox.Show(xx.body.innerHTML);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

甚至像这样的代码:

MessageBox.Show(myDocument.frames.length.ToString());

给出了相同的强制类型转换异常

点击iframe内的按钮

这应该可以通过iframe元素的contentWindow或contentDocument属性,具体取决于您根据http://www.w3schools.com/jsref/prop_frame_contentwindow.asp拥有的IE版本。然而,我没能让它工作,contentWindow似乎返回一些元素,但它具有空文档属性(在我的情况下与IE9)。

既然这个问题已经发布了一段时间了,我想知道你是否设法在这方面取得了一些进展?

尝试使用单线程架构(STA线程)访问帧集合。在确认框架内容的功能单元测试中,这对我来说很有效。在切换线程模型之前,我有相同的强制转换错误。