Feature proposal: find element by javascript?

147 views
Skip to first unread message

mrstevegross

unread,
Nov 8, 2011, 10:27:18 AM11/8/11
to Selenium Developers
Hi folks... Currently, you can find elements by a few basic methods:
id, xpath, attribute, etc. However, in some cases it's handy to find
an element by arbitrary logic. For instance, I want to find an element
(1) with 3 child nodes, (2) having attribute foo=bar, and (3) whose
name is an anagram of SpecialType. I figured out how to use
driver.ExecuteScript to carried out this logic in javascript. It looks
a bit like this:

=== Javascript snippet ===
function elementFound(elem) {

var nodeType = navigator.appName == ""Microsoft Internet
Explorer"" ? document.ELEMENT_NODE : Node.ELEMENT_NODE;

if(elem.nodeType == nodeType)
{
/* Element identification logic here */
}
else { return false; }
}

function traverseElement(elem) {
if (elementFound(elem) == true) {
return elem;
}
else {
for (var i = 0; i < elem.childNodes.length; i++) {
var ret = traverseElement(elem.childNodes[i]);
if(ret != null) { return ret; }
}
}
}

return traverseElement(document);
=== EOF ===

I think it would be really handy if I could express this logic in a
By() instance. Maybe something like this:
driver.FindElement(By.Js("/* Element identification logic here */");

What do y'all think? Would this be handy? It doesn't seem like it
would be too hard to implement...

Thanks,
--Steve

Jason Leyba

unread,
Nov 8, 2011, 12:14:57 PM11/8/11
to selenium-...@googlegroups.com
You could always implement a helper method with your own JS locator strategy.  As for adding it to the core set of locators, I don't see a problem with it (I've already added it for the JS bindings).  The locator should check that the resulting value is actually a WebElement and thrown an error if it's not.


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


mrstevegross

unread,
Nov 8, 2011, 12:25:56 PM11/8/11
to Selenium Developers

> You could always implement a helper method with your own JS locator
> strategy.  As for adding it to the core set of locators, I don't see a
> problem with it (I've already added it for the JS bindings).  The locator
> should check that the resulting value is actually a WebElement and thrown
> an error if it's not.

1. Any advice on how/where to add this functionality in the Selenium
codebase? I'm still new to it...
2. The current implementation simply returns null if a webelement
isn't found.

Thanks,
--Steve

Daniel Wagner-Hall

unread,
Nov 18, 2011, 3:09:58 PM11/18/11
to selenium-...@googlegroups.com
On 8 November 2011 17:25, mrstevegross <mrstev...@gmail.com> wrote:
> 1. Any advice on how/where to add this functionality in the Selenium
> codebase? I'm still new to it...

Have a look in javascript/atoms/locators - that should at least help
you for firefox

Jason Leyba

unread,
Nov 21, 2011, 3:55:45 PM11/21/11
to selenium-...@googlegroups.com
Defining a new locator strategy in the atoms would expose the functionality in firefox, chrome*, android, iphone, and opera*.  I don't believe IE uses the atoms for locating elements (Jim?)

* While Chrome and Opera use the atoms, they are maintained by the browser vendors. Any change in the atoms could take a while to appear in those drivers.

Jim Evans

unread,
Nov 21, 2011, 4:14:45 PM11/21/11
to Selenium Developers
For the record, IE uses the atoms for all except XPath and CSS
Selectors. A case can be made that the IE driver shouldn't use the
atoms because of the IE's atrocious JavaScript performance[1], but
that's how it's coded at the moment.

--Jim

[1] I think the "use common code where possible" principle gives us
more win than the perf hit, but I can't speak authoritatively about
that, because I haven't measured it, and I don't feel confident
talking about performance without numbers to back up any assertions I
might make.

On Nov 21, 3:55 pm, Jason Leyba <jmle...@gmail.com> wrote:
> Defining a new locator strategy in the atoms would expose the functionality
> in firefox, chrome*, android, iphone, and opera*.  I don't believe IE uses
> the atoms for locating elements (Jim?)
>
> * While Chrome and Opera use the atoms, they are maintained by the browser
> vendors. Any change in the atoms could take a while to appear in those
> drivers.
>

> On Fri, Nov 18, 2011 at 12:09 PM, Daniel Wagner-Hall <dawag...@gmail.com>wrote:


>
>
>
> > On 8 November 2011 17:25, mrstevegross <mrstevegr...@gmail.com> wrote:
> > > 1. Any advice on how/where to add this functionality in the Selenium
> > > codebase? I'm still new to it...
>
> > Have a look in javascript/atoms/locators - that should at least help
> > you for firefox
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Selenium Developers" group.
> > To post to this group, send email to selenium-...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > selenium-develo...@googlegroups.com.
> > For more options, visit this group at

> >http://groups.google.com/group/selenium-developers?hl=en.- Hide quoted text -
>
> - Show quoted text -

Reply all
Reply to author
Forward
0 new messages