One other way is to use the element method 'attribute_value'

so assuming your first level child divs have some attribute,

<div id=1>
  <div xyz='abc'/>
  <div xyz='def'>
     <div/>
     <div/>
   </div>
   <div/>
</div>

this code will work

ie.div(:id, 1).divs do |child|
  if child.attribute_value('xyz') =~ /[a-z0-9]+/ # the + on the regexp
makes sure its not mathing blank xyz's
    pp child
  end
end

the best part is, the attributes on your top level child divs can be
anything,
using it at the moment to get a dojo attribute called 'widgetid'
Its working under firefox, have yet to test it with IE


On Apr 18, 1:20 am, Paul Rogers <paul.rog...@shaw.ca> wrote:
> this might be simpler:
>
> top_div = ie.div(:id , 'toplevel')
> top_div.divs.each do | a_child |
>     next unless a_child.document.parentElement.invoke( "id") == top_div.id
>
>    puts "an immediate child is " + a_child.id
> end
>
> untested, but it seems sound, as long as you have useful identifiers of some
> sort. And if you havent, you can kind of cheat and add a name or id to the
> top level one. Im not sure if you an do that directly with watir or if Ive
> had to do some hackery to do it.
>
> Paul
>
> On Fri, Apr 17, 2009 at 4:55 PM, marekj <marekj....@gmail.com> wrote:
>
> > I never needed to solve this problem but if I may nudge you towards this:
> > 1. get the base div.html
> > 2. and pump it to Hpricot for processing
> > and maybe use 'children_of_type "div" call to get to divs you need.
> > a wild suggestion since purely theoretical.
>
> > marekj
>
> > Watirloo: Semantic Page Objects in UseCases
> >http://github.com/marekj/watirloo/
>
> > On Tue, Apr 14, 2009 at 1:02 PM,  <andrew.d...@lthree.com> 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 <michael_h...@yahoo.com> wrote:
> > >> Can you try xpath?
>
> > >> ----- Original Message ----
> > >> From: "andrew.d...@lthree.com" <andrew.d...@lthree.com>
> > >> To: Watir General <watir-general@googlegroups.com>
> > >> Sent: Tuesday, April 14, 2009 7:54:53 AM
> > >> Subject: [wtr-general] First level children 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 any level. 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 the first level children.
>
> > >> 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 watir-general@googlegroups.com
Before posting, please read the following guidelines: 
http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to 
watir-general-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/watir-general
-~----------~----~----~----~------~----~------~--~---

Reply via email to