I faced the same problem too. I had to create a function to solve this
problem
Basically recurse creates a hash of all the divs from the root div.
I had something similar to:
<div>
   <div>
   <div>
   <div>
         <div>
         <div>
               <div>
   <div>
        <div>
        <div>
        <div>
              <div>
                   <div>
                   <div>
<\div>



# recurse will create a hash map of all the divs:
# I have tested upto three levels:

def recurse(y,divHash,count)
  y.divs.each do |x|
      divHash1 = Hash.new
      if x.parent == y
        count = count + 1
        divHash[count] =  x.divs.length > 0 ? recurse(x,divHash1,-1) :
x
     end
  end
  return divHash
end



#Main code.
divHash = Hash.new
count = -1
recurse(IE.form(:name,"FormId").div(:index,1),divHash,count)
#div(:index,1) is my root div


#divHash will have a hash map of all the n level divs.
#Sorting according to the order they appear on the page.

divArray = Array.new

divHash.each_pair {|key, value|
  divArray[key] = value
}
#You can pick and choose the first level divs you want from the
divArray e.g. divArray[0] , etc.


On Apr 14, 2:02 pm, [email protected] wrote:
> How can xpath be used to return an array of divs? What would the xpath
> look like?
>
> On Apr 14, 10:30 am, Michael Hwee <[email protected]> wrote:
>
> > Can you try xpath?
>
> > ----- Original Message ----
> > From: "[email protected]" <[email protected]>
> > To: Watir General <[email protected]>
> > Sent: Tuesday, April 14, 2009 7:54:53 AM
> > Subject: [wtr-general]Firstlevelchildren only
>
> > I have a large tree of nested divs, and I want to get an array of just
> > the direct children of any particular div. I do not know ahead of time
> > how many divs there will be at anylevel. Starting from a div, what is
> > the Watir code for getting the divs that are direct children (and yes,
> > there are other element types mixed in throughout)?
>
> > Example
>
> > <div id=1>
> >   <div/>
> >   <div>
> >      <div/>
> >      <div/>
> >    </div>
> >    <div/>
> > </div>
>
> > divs() returns the entire list of all descendant div elements. I want
> > just thefirstlevelchildren.
>
> > Andy- Hide quoted text -
>
> > - Show quoted text -

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To post to this group, send email to [email protected]
Before posting, please read the following guidelines: 
http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/watir-general
-~----------~----~----~----~------~----~------~--~---

Reply via email to