Access a span inside iframe using mshtml
Asked Answered
B

0

2

Im creating an IE BHO and trying to access a page that has multiple iframe tags. How to access the text in a span with id="messageText-txt" which is inside a specific iframe with name="isolatedWorkArea" and id="isolatedWorkArea" using mshtml.

I tried the following options but none of them returns me the text. Any idea?

Option 1:

void BrowserEvents_NavigateComplete2(object pDisp, ref object URL)
{
  SHDocVw.InternetExplorer currentIEWin = pDisp as SHDocVw.InternetExplorer;
  mshtml.IHTMLDocument2 docObject = currentIeWin.Document as IHTMLDocument2;
  mshtml.FramesCollection iframesCollection = docObject.frames;
  for (int i = 0; i <= iframesCollection.length; i++)
  {
    mshtml.HTMLDocument iFrameDocObject = iframesCollection.item(i).document as HTMLDocument;
    if (iFrameDocObject != null)
    {
      IHTMLElement spanMessageText = iFrameDocObject.getElementById("messageText-txt");
      if (!string.IsNullOrEmpty(spanMessageText.innerHTML))
      {
        spanText = spanMessageText.innerHTML;

Option 2:

void BrowserEvents_NavigateComplete2(object pDisp, ref object URL)
{
  SHDocVw.InternetExplorer currentIEWin = pDisp as SHDocVw.InternetExplorer;
  mshtml.IHTMLDocument3 docObject2 = currentIeWin.Document as IHTMLDocument3;
  IHTMLElement Iframe = docObject2.getElementById("isolatedWorkArea");
  if (Iframe != null)
  {
    mshtml.HTMLDocument iFrameDocObject = Iframe.document as HTMLDocument;
    if (iFrameDocObject != null)
    {
      IHTMLElement spanMessageText = iFrameDocObject.getElementById("messageText-txt");
      if (spanMessageText != null)
      {
        if (!string.IsNullOrEmpty(spanMessageText.innerHTML))
        {
          spanText = spanMessageText.innerHTML;

Option 3:

void BrowserEvents_NavigateComplete2(object pDisp, ref object URL)
{
  SHDocVw.InternetExplorer currentIEWin = pDisp as SHDocVw.InternetExplorer;
  mshtml.IHTMLDocument3 docObject2 = currentIeWin.Document as IHTMLDocument3;
  IHTMLElementCollection frames = (IHTMLElementCollection)docObject2.getElementsByTagName("frame");
  if (frames != null)
  {
    foreach (HTMLIFrame frm in frames)
    {
     if (frm != null)
     {
      mshtml.HTMLDocument iFrameDocObject = frm.document as HTMLDocument;
      if (iFrameDocObject != null)
      {
          IHTMLElement spanMessageText = iFrameDocObject.getElementById("messageText-txt");
          if (spanMessageText != null)
          {
             if (!string.IsNullOrEmpty(spanMessageText.innerHTML))
             {
                spanText = spanMessageText.innerHTML;
Binette answered 7/1, 2014 at 6:22 Comment(3)
What if you try the above options on DocumentComplete on the Main frame?Nawab
Hi Manuell, theres no difference it works the same for both DocumentComplete and NavigateComplete2.Binette
You should use debugger and/or logging: Does the "Option 1" retrieves all the frames? Do you successfully get an HTMLDocument for each Frame?Nawab

© 2022 - 2024 — McMap. All rights reserved.