Link to home
Start Free TrialLog in
Avatar of salinasj
salinasj

asked on

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

**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!
Avatar of Marshawk
Marshawk

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")
Avatar of salinasj

ASKER

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?
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?
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
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.
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.  :(

k. thanks Scott.
I've gotta go home now, so will check after about an hour.

I really appreciate the help!  ;)

-Jim
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.
ASKER CERTIFIED SOLUTION
Avatar of Marshawk
Marshawk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
DiveBlue,
salinasj has made a question for you to collect some points for you assistance on this question.  It is at:
https://www.experts-exchange.com/questions/20543558/Points-for-DiveBlue-re-20542984.html

SpideyMod
Community Support Moderator @Experts Exchange