?
Solved

*URGENT* How can I get Frame(1)  data after Frame(0) link clicked?

Posted on 2003-03-07
11
Medium Priority
?
592 Views
Last Modified: 2012-06-27
**URGENT HELP REQUIRED**

How can I get a known element from a document in frame 1 after having clicked a known link in frame 0 using WebBrowser control?

I have this:
Sub GetData(DestURL As String)
   Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")
   Dim wPage As HTMLDocument
   Dim pRes As String
   ' Make browser visible for debugging
   WebBrowser.Visible = True
   ' Navigate to page
   WebBrowser.navigate("http://" & DestURL)
   Do While WebBrowser.busy
      DoEvents
   Loop
   Set wPage = WebBrowser.document
   ' Click first link in frame(0)
   wPage.frames(0).document.links(0).Click
   '
   ' HOW do I grab the value of element "MYFIELD" that has
   ' just loaded in frame(1) after I clicked the link in frame(0) ??
   '
   MsgBox "Value: " & pRes
End Sub

In viewing the visible browser, I can see that the new page is indeed loading in frame 1
after the click event fires.

And I've tried many permutations of
   pRes = wPage.frames(1).All("MYFIELD").Item.Value
but just can't seem to get it here...

The error I am getting is "Object doesn't support this property or method"

Any help would be greatly appreciated!
0
Comment
Question by:salinasj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 3

Expert Comment

by:Marshawk
ID: 8091554
I Think since your wPage.frames(0).document.links(0).Click is causing another document to load you will need another:

  Do While WebBrowser.busy
     DoEvents
  Loop

after your wPage.frames(0).document.links(0).click And before you try to reference:

 pRes = wPage.frames(1).All("MYFIELD").Item.Value

Probably you are trying to access frame1 before it has loaded and before its MSHTML HTMLDocument property is useable.

Use the url property in your documentcomplete event to see what is coming thru and when.

Plus if its not that try this syntax:
oBrowser.document.All.Item("MYFIELD")
0
 
LVL 1

Author Comment

by:salinasj
ID: 8091698
Nope.
that didn't do it either...

just now tried :
   wPage.frames(0).document.links(0).click
   Do While WebBrowser.busy
      DoEvents
   Loop
   Sleep 1000    ' Just givin it another second
   Set wPage = WebBrowser.document
   Sleep 1000    ' Just givin it another second
   pRes = wPage.frames(1).document.body.innerHTML
   MsgBox "HTML: " & vbCrLf & pRes

This DOES return the innerHTML of frame(1)
but it still won't let me getelementsbyname("MYFIELD").Item.Value

See anything I might be missing here?
0
 
LVL 3

Expert Comment

by:Marshawk
ID: 8091822
okay...so "MsgBox "HTML: " & vbCrLf & pRes" is showing that you have the right html page, correct?

If so then how bout:
Dim oElement as Object
Set oElement = wPage.getElementsByName("MYFIELD")

then see what
    msgbox oElement(0).Value
shows

Still not good. Then (sorry) are you sure about the name of the html element. You have checked the source code for spelling etc.?

What type of element is "MYFIELD" ?

Is it embedded in a form?
Dim oForm As Object
Set oForm = oBrowser.document.Forms(YourIndex)
then does msgbox oForm.MYFIELD do anything?

Is there any chance that frame1 has another frame or i-frame in it?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Diveblue
ID: 8091856
salinasj,
  this will get you past your first problem. I'll try to get back to you on ho to get the known element

Scott

Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")
  Dim wPage As HTMLDocument
  Dim hw2 As HTMLWindow2
 
  Dim pRes As String
  ' Make browser visible for debugging
  WebBrowser.Visible = True
  ' Navigate to page
  WebBrowser.navigate ("http://" & url)
  Do While WebBrowser.busy
     DoEvents
  Loop
  Set wPage = WebBrowser.document
  ' Click first link in frame(0)
  If wPage.frames.length = 0 Then wPage.links(, 0).Click
   If wPage.frames.length > 0 Then
       Set hw2 = wPage.frames.Item(0)
       If hw2.document.links.length > 0 Then
         hw2.document.links(, 0).Click
       End If
   End If
0
 
LVL 1

Author Comment

by:salinasj
ID: 8091909
Now it says : Object variable or With Block variable not set

Here's the exact code I'm using:
I cut out all the floof & have this basic code

   WebBrowser.document.frames(0).document.links(0).Click
   Do While WebBrowser.busy
      DoEvents
   Loop
   Sleep 1000
   Dim oElement As Object
   Set oElement = WebBrowser.document.frames(1).document.getElementsByName("SYSNAME")
   MsgBox oElement(0).Value

Now, in watching the open (visible) browser during execution, I can see it load the initial page, then i see it load the new page into frame(1) after the click.
0
 
LVL 1

Author Comment

by:salinasj
ID: 8091938
Just for grins, I changed it to this to see if it is seeing the right page, and it is.

MsgBox "About to click"

  WebBrowser.document.frames(0).document.links(0).Click
  Do While WebBrowser.busy
     DoEvents
  Loop
  Sleep 1000

MsgBox "After click and after sleep"
MsgBox WebBrowser.document.frames(1).document.body.innerHTML

  Dim oElement As Object
  Set oElement = WebBrowser.document.frames(1).document.getElementsByName("SYSNAME")
  MsgBox oElement(0).Value

the element "MYFIELD" is a SELECT tag within a form.
If I access the webpage directly (instead of through the frames like above) I can use getElementsByName("MYFIELD").Item.Value
to get it.
but I can't do it like that because when I do, the page has javascript code in it that references the parent page and other frames. and this causes javascript errors that screw with the app.  :(

0
 
LVL 1

Author Comment

by:salinasj
ID: 8091969
k. thanks Scott.
I've gotta go home now, so will check after about an hour.

I really appreciate the help!  ;)

-Jim
0
 
LVL 1

Author Comment

by:salinasj
ID: 8092735
Oops!
I didn't see there were two people helping me...
Thanks to both of you.

Marshawk, in answer to your last question:
yes. it does display the innerHTML of frame(1) properly

I tried doing the oElement as Object, wPage.getElementsbyName("MYFIELD") but it gave me this error: Object variable or With Block variable not set
whenever i try to access oElement(0).Value

The name spelling of the html element is correct.
the "MYFIELD" element is a form SELECT field.
I also tried your example for a form object, but that yielded the same results.

There are no other frames within frame1.
There are no i-frames within frame1

There ARE 2 forms within frame1, but this element is in the first form

DiveBlue,
Thanks for the alternate way to traverse the frames.
It is more efficient than the way I was planning to do it.
And am anxiously awaiting your returned help with getting the known element from the newly loaded page in frame(1) after the click event.
0
 
LVL 3

Accepted Solution

by:
Marshawk earned 2000 total points
ID: 8092962
Okay here is the deal. A Select object does not have value field. You need to cycle thru the select objects option collection and see which one has its selected property set. Then you can access either the "text" or the "value" field of the selected option collection member. Depending on how the select statement is HTML coded your data may be in the text or the value property.



'reference the Microsoft HTML object Library first
'declare these then the VB intellisense will work so
'you can see the properties and methods when you type in
'the dot after the object name in your code.

Dim oSelectCollection As IHTMLElementCollection
Dim oSelectElement As HTMLSelectElement
Dim oOptionElement As HTMLOptionElement

'cycle thru the collection till you get your select box
Set oSelectCollection = oDoc.getElementsByTagName("SELECT")



For each oSelectElement in oSelectCollection
  if oSelectElement.name = "MYFIELD" Then
     Exit for ' cause have your object
  End if
next

For Each oOptionElement In oSelectElement
    If oOptionElement.Selected Then MsgBox oOptionElement.Text ( or oOptionElement.value)
     
Next

So once you get your select object you must check which option in the options collection has its selected property set. Then you can get what you want by referencing the text or value property of the option object.

I tested this and it works on my system with html page with a select object in it.

REMEMBER THE object returned by wPage.getElementsByName("MYFIELD") in your code is a HTMLSelectElement object which DOES NOT HAVE A VALUE PROPERTY! Cycle the options collection of wPage.getElementsByName("MYFIELD") for the option object that is selected (This is the one that is visible in the select object) and then access the option object's text or value object.

Does that make any sense?


0
 
LVL 1

Author Comment

by:salinasj
ID: 8093097
SWEET!
You just made my weekend!
You get the 500pts and an A for awesome support!

Although DiveBlue did also enlighten me with a much better way to traverse the frames within the set...
I think he deserves credit for that.
I'll post in CS requesting an adjustment be made towards his efforts as well.

Again,
Thank You for your assistance!

-Jim
0
 

Expert Comment

by:SpideyMod
ID: 8095281
DiveBlue,
salinasj has made a question for you to collect some points for you assistance on this question.  It is at:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20543558.html

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question