在 iframe 中的元素上使用 getElementById [英] getElementById on element within an iframe

查看:35
本文介绍了在 iframe 中的元素上使用 getElementById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的代码适用于 iframe 之外的元素.我应该如何使用 getElementById 在 iframe 中获取元素?我的最终目标是在 <body id="tinymce"><p>...</p></body> 标签中写入文本.我没有使用 webBrowser 控件 - 这是用于 iexplore 的外部实例

My current code works on elements outside of an iframe. How should I approach fetching elements within an iframe using getElementById? My end goal is to write text within the the <body id="tinymce"><p>...</p></body> tags. I am not using a webBrowser control - this is for an external instance of iexplore

HTML 示例

代码示例

foreach (InternetExplorer ie in new ShellWindowsClass())
{
    if (ie.LocationURL.ToString().IndexOf("intranet_site_url") != -1)
    {
        IWebBrowserApp wb = (IWebBrowserApp)ie;
        while (wb.Busy) { Thread.Sleep(100); }
        HTMLDocument document = ((HTMLDocument)wb.Document);

        // FETCH BY ID
        IHTMLElement element;
        HTMLInputElementClass hitem;

        element = document.getElementById("tinymce");
        hitem = (HTMLInputElementClass)element;
        hitem.value = first_name;

        // FETCH BY ID in IFRAME
        IHTMLFramesCollection2 hframes = document.frames;
        for (int i = 0; i < hframes.length; i++)
        {
            object ref_index = i;
            IHTMLWindow2 currentFrame = (IHTMLWindow2)hframes.item(ref ref_index);

            if (currentFrame != null)
            {
                MessageBox.Show(currentFrame.name);
                // what to do from here?
            }
            else
                MessageBox.Show("Null");
        }
    }
}

- 更新想法有机会在下面调整我的想法吗?

- update idea Chance of adapting my idea below?

if (currentFrame != null)
{
    MessageBox.Show(currentFrame.name);

    HTMLDocument document_sub = ((HTMLDocument)currentFrame.document);
    IHTMLElement element_sub;
    HTMLInputElementClass hitem_sub;

    element_sub = (document_sub.getElementById("tinymce"));
    hitem_sub = (HTMLInputElementClass)element_sub;
    try
    {
        hitem_sub.value = first_name;

        // the above will produce...
        // InvalidCastException: Unable to cast COM object of type 'mshtml.HTMLBodyCLass' to class type 'mshtml.HTMLInputElementClass'
    }
    catch { }
}

推荐答案

试试这个:

Windows.Forms.HtmlWindow frame = WebBrowser1.Document.GetElementById("decrpt_ifr").Document.Window.Frames["decrpt_ifr"];
HtmlElement body = frame.Document.GetElementById("tinymce");
body.InnerHtml = "Hello, World!";

它获取框架并将其视为不同的文档(因为它是),然后尝试从其 id 中获取元素.祝你好运.

That gets the frame and treats it as a different document (because it is) and then it tries to get the element from its id. Good luck.

这应该可以利用 dynamic 数据类型和 InternetExplorer 接口:

This should do the trick taking advantage of the dynamic datatype, and InternetExplorer interface:

private void Form1_Load(object sender, EventArgs e)
{
    foreach (InternetExplorer ie in new ShellWindows())
    {
        if (ie.LocationURL.ToString().IndexOf("tinymce") != -1)
        {
            IWebBrowserApp wb = (IWebBrowserApp)ie;
            wb.Document.Frames.Item[0].document.body.InnerHtml = "<p>Hello, World at </p> " + DateTime.Now.ToString();
        }
    }
}

这篇关于在 iframe 中的元素上使用 getElementById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆