使用mshtml访问iframe中的span

时间:2014-01-07 06:22:24

标签: c#-4.0 iframe bho mshtml

我正在创建IE BHO并尝试访问具有多个iframe标记的页面。如何使用id =“messageText-txt”访问带有id =“messageText-txt”的文本中的文本,该文本位于具有name =“isolatedWorkArea”和id =“isolatedWorkArea”的特定iframe中,使用mshtml。

我尝试了以下选项,但没有一个将文本返回给我。有什么想法吗?

选项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;

选项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;

选项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;

0 个答案:

没有答案