Finding all Children from a WebElement

3,582 views
Skip to first unread message

James

unread,
Feb 21, 2012, 9:27:29 PM2/21/12
to webdriver
Hi All,

Sorry to bother you all again. I was wondering if anyone had found
all child elements from a already found WebElement. Unfortunately I'm
dealing with a table which varies in size and types of elements.

At the moment I am using table.findElements(By.xpath("*"));

The problem with the above is that it takes a long time to find all
the child elements.

I would really like to use jquery and do something like

ArrayList<WebElement> tableChildren = (ArrayList<WebElement>)
((JavascriptExecutor) selenium).executeScript("return
arguments[0].children();", table);

But I don't think I can convert the return type to that of
List<WebElement>, does anyone know how to do this?

Thanks again,
James

Simon Stewart

unread,
Feb 22, 2012, 6:05:49 AM2/22/12
to webd...@googlegroups.com
You can certainly return List<WebElement> from the javascript executor
iff the return type of the JS is an array containing DOM elements. If
that throws an exception use the js "typeof" operator to dig a little
deeper into the problem (you may find that you need to build an array
yourself)

Simon

> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>

darrell

unread,
Feb 22, 2012, 2:48:40 PM2/22/12
to webdriver
Have you tried using cssSelector rather than xpath? I.e.
table.findElements(By.cssSelector("*"));

James

unread,
Feb 22, 2012, 4:08:45 PM2/22/12
to webdriver
Unfortunately by.cssSelector("*") takes even longer than the xpath
version (strange)....

As for your idea Simon, it seems as though I can't actually use
'return typeof arguments[0].children();' or 'return
arguments[0].children();' Both through a webdriver exception. I will
try out some other methods and see how I go.

James

unread,
Feb 22, 2012, 5:54:14 PM2/22/12
to webdriver
Sorry not a webdriver exception a class cast exception with details
com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to
java.util.List exception.

I am going to try to put them in an array myself and if that doesn't
work I might just grab children and iterate through.

Thanks,
James

James

unread,
Feb 22, 2012, 10:26:56 PM2/22/12
to webdriver
Well I managed to get a solution to work but it is pretty ugly.

I still seem to have an issue when 'injecting' jquery onto a page
which already has some jquery but not version I am injecting. Has
anyone dealt with this that after injecting jquery all javascript/
jquery functions that were on the page no longer run?

Cheers,
James

Also happy to post the answer if people are interested but I wouldn't
use it unless you have no other workarounds...

Simon Stewart

unread,
Feb 23, 2012, 6:53:26 AM2/23/12
to webd...@googlegroups.com
I was suggesting finding the type of the collection in pure JS rather
than in a webdriver test :) I suspect that you're dealing with
something that isn't actually an array, or that doesn't actually
contain DOM elements. If that's the case, the JS executed in the test
will need to iterate over the children and populate its own array to
return.

Simon

Maxim Vorobev

unread,
Feb 23, 2012, 7:18:48 AM2/23/12
to webd...@googlegroups.com
If you use IE try this js
return arguments[0].childNodes;

2012/2/23, Simon Stewart <simon.m...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages