getElementById on element within an iframe

前端 未结 4 1164
天命终不由人
天命终不由人 2020-12-05 16:33

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 th

4条回答
  •  半阙折子戏
    2020-12-05 17:31

    Try this:

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

    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.

    Edit: 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 = "

    Hello, World at

    " + DateTime.Now.ToString(); } } }

提交回复
热议问题