Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi, I use the following code to navigate to a certain website

 

obIE(i) = New SHDocVw.InternetExplorer()

obIE(i).Navigate(address)

obIE(i).Visible = True

 

Now I want to read the text from the page. I know about innertext which works fine unless there is a frame on the page. How can I read the text from a certain frame?

  • Leaders
Posted

the best i could come up with after a bit of messing is this, hope it helps...

       If Not doc Is Nothing Then
           Dim x As Integer = 0
           Dim frm As mshtml.HTMLWindow2Class
           For x = 0 To doc.frames.length - 1
               frm = DirectCast(doc.frames.item(x), mshtml.HTMLWindow2Class)
               MessageBox.Show(frm.document.activeElement.innerText)
           Next
       End If

  • 2 weeks later...
Posted (edited)

Hallo,

 

I use also the SHDocVw.InternetExplorer() in my ASP.NET (C#) applikation, and want to get the mshtml.FramesCollection, but I get every time an exception. The value of the InternetExplorer.Document.frames is: <error: an exception of type: {System.InvalidCastException} occurred>

 

Abstract:

//constructor

InternetExplorerClass mIE = new InternetExplorerClass();

mIE.Visible = true;

//...

mIE.Navigate2(ref objURL, ref a, ref a, ref a, ref a);

 

// DocumentComplete

if(URL.ToString().Equals(url)) {

Console.WriteLine(mIE.Document.ToString());

mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2) mIE.Document;

Console.WriteLine(doc.frames.length);

}

[code=csharp]

 

This are the tag�s of my Frameset:

[code]

<frameset cols="*,*">

<frame name="left" src="WebForm1.aspx">

<frame name="right" src="WebForm2.aspx">

</frameset>

[code]

 

I don't know why I get the error?

Edited by jimmknopf
  • 1 year later...
Posted (edited)

Access the the InternetExplorer.Document at the right time and from the right thread

 

Hi,

 

After spending the whole day on it I have found a solution.

 

http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?FeedbackId=25ab57f0-4d1c-45d4-ac7f-be8061a2ec58#Workarounds

 

You have to read InternetExplorer.Document from the main thread. Please note the use of STAThread. Read more about it here:

http://blogs.msdn.com/jfoscoding/archive/2005/04/07/406341.aspx

 

In the example below I know that there are three frames and I want to be sure that they are all loaded before I access them (This is another problem you have to avoid...)

 

/Patrick

 

 

using System;

 

namespace ConsoleApplication7

{

/// <summary>

/// Summary description for Class1.

/// </summary>

using System;

using mshtml;

using SHDocVw;

using System.Collections;

using System.Threading;

 

namespace ConsoleApplication1

{

class Class1

{

static InternetExplorer ie = null;

 

[sTAThread]

static void Main(string[] args)

{

Class1 class1 = new Class1();

class1.Test();

Console.Read();

}

 

AutoResetEvent firstDocumentCompleted;

AutoResetEvent lastDocumentCompleted;

int completedDocuments;

int expectedDocuments;

 

public void Test()

{

Console.WriteLine("Starting:" + Thread.CurrentThread.ApartmentState.ToString());

 

ie = new SHDocVw.InternetExplorer();

ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(DocumentComplete);

ie.Visible = true;

firstDocumentCompleted = new AutoResetEvent(false);

lastDocumentCompleted = new AutoResetEvent(false);

 

 

completedDocuments = 0;

expectedDocuments = 4;

object o = null;

ie.Navigate("http://www.htmq.com/html/sample/frame.htm", ref o, ref o, ref o, ref o);

firstDocumentCompleted.WaitOne(30000, false);

 

IHTMLDocument2 document = (IHTMLDocument2)ie.Document;

lastDocumentCompleted.WaitOne(30000, false);

 

IHTMLFramesCollection2 frames = (IHTMLFramesCollection2)document.frames;

for (int index = 0; index < frames.length; index++)

{

object i = index;

frames.item(ref i);

 

IHTMLWindow2 window = (IHTMLWindow2)frames.item(ref i);

IHTMLDocument2 frameDocument = (IHTMLDocument2)window.document;

 

Console.WriteLine(frameDocument.body.innerHTML);

}

 

Console.WriteLine();

}

 

public void DocumentComplete(object pDisp, ref object URL)

{

Console.WriteLine("DocumentComplete:" + Thread.CurrentThread.ApartmentState.ToString());

int i = Interlocked.Increment(ref completedDocuments);

 

if (i == 1)

{

firstDocumentCompleted.Set();

}

 

if (i == expectedDocuments)

{

lastDocumentCompleted.Set();

}

 

 

}

}

}

}

Edited by rede

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...