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;
  • What if you try the above options on DocumentComplete on the Main frame? – manuell Jan 13 '14 at 10:19
  • Hi Manuell, theres no difference it works the same for both DocumentComplete and NavigateComplete2. – user3167749 Jan 13 '14 at 11:20
  • You should use debugger and/or logging: Does the "Option 1" retrieves all the frames? Do you successfully get an HTMLDocument for each Frame? – manuell Jan 13 '14 at 13:44

0 Answers0