Using "class" locator with Selenium2Library

7,677 views
Skip to first unread message

Ryan Oakes

unread,
Oct 5, 2016, 4:36:51 PM10/5/16
to robotframework-users
I'm currently learning Selenium2Library and I noticed that, on the list of locators, class isn't an option (http://robotframework.org/Selenium2Library/Selenium2Library.html#Introduction).  

I'm building a Selenium suite that will hit an ever-changing website, so using xpath guidance is really not ideal.

I found one post within this group (https://groups.google.com/forum/#!topic/robotframework-users/ypbASWD2KGM) that addresses the issue but the solution seems to invoke the xpath (Click Element | xpath=//div[@class="value"), so that doesn't really help me! 

Any ideas? I noticed there is a custom locator but I'm a little confused by it and not sure if it can utilize class. 

Any help would be greatly appreciated! Thanks. 

Tatu Aalto

unread,
Oct 6, 2016, 2:24:53 AM10/6/16
to oakes...@gmail.com, robotframework-users

Ugh

The selenium does support finding elements by class[1], but you are correct and that functionality is not directly supported by the Selenium2Library. This could be good addition to the Selenium2Library and you could raise an issue about it. Even better if an pull request could be made too.

But you can find element by using xpath and targeting only the class name. If you have only one class name in the element then this works nicely:

//*[@class="value"]

But if you have multiple classes in a single element, you need to do something like this:

//*[contains(concat(' ', @class, ' '), ' value ')]

Last example is copy/pasted from stack overflow: http://stackoverflow.com/questions/1604471/how-can-i-find-an-element-by-css-class-with-xpath

-Tatu
Send from my mobile
[1] http://selenium-python.readthedocs.io/locating-elements.html?highlight=Find%20by%20class#locating-elements-by-class-name


--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.
To post to this group, send email to robotframework-users@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Ryan Oakes

unread,
Oct 6, 2016, 11:06:24 AM10/6/16
to robotframework-users, oakes...@gmail.com
Thanks Tatu, I appreciate the response! I'll give that xpath/class combo a shot. 

Is the "//*[@class="value"]" method you're utilizing using * to select everything up to that point to find the class specifically? If so, it seems like that's a good way for it to find the class even if some XML changes occur (which is ideal). 

Thanks for the help!
To post to this group, send email to robotframe...@googlegroups.com.

Pekka Klärck

unread,
Oct 7, 2016, 5:08:51 AM10/7/16
to Tatu Aalto, oakes...@gmail.com, robotframework-users
2016-10-06 9:24 GMT+03:00 Tatu Aalto <aalto...@gmail.com>:
>
> The selenium does support finding elements by class[1], but you are correct
> and that functionality is not directly supported by the Selenium2Library.
> This could be good addition to the Selenium2Library and you could raise an
> issue about it. Even better if an pull request could be made too.

Adding a separate locator type to find elements by a class could be
handy and ought to be pretty easy to implement.

> But you can find element by using xpath and targeting only the class name.
> If you have only one class name in the element then this works nicely:
>
> //*[@class="value"]
>
> But if you have multiple classes in a single element, you need to do
> something like this:
>
> //*[contains(concat(' ', @class, ' '), ' value ')]

Wouldn't CSS selectors work better here? I've never tested them in
this particular context, but simply using `css=.class-name` as the
locator ought to be enough. If that works, then perhaps we don't need
a separate locator type for classes but could just document this usage
better.

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Tatu Aalto

unread,
Oct 7, 2016, 9:16:17 AM10/7/16
to Pekka Klärck, oakes...@gmail.com, robotframework-users

Ugh

The star in xpath is a wild card and will match any element. The xpath is quite powerful and it can actually do very complex things (In my mind xpath is like Perl, very powerful and most of the time very write once). In any case, reading some tutorials[1] might be useful.

The css selector looks handy and better than the xpath. I mostly use xpath, because of the nature of my application and some limitations of the css selector. But if you only need to select by a class, I would definitely use css selector, because it is shorter to write and therefore easier to read.

The exposing the selenium class selector could be useful also on Selenium2Library side. Perhaps raising a issue and thinking on the issue which way to go is the best option.

-Tatu
Send from my mobile

[1] http://www.w3schools.com/xsl/xpath_syntax.asp

Reply all
Reply to author
Forward
0 new messages