Access a span inside iframe using mshtml

旧街凉风 提交于 2020-01-04 07:18:12

问题


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;

来源:https://stackoverflow.com/questions/20965171/access-a-span-inside-iframe-using-mshtml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!