[ruby] Using regular expressions to select an element

984 views
Skip to first unread message

sbtang

unread,
Nov 12, 2010, 12:39:20 PM11/12/10
to Selenium Users
Hi there,

I am relatively new to selenium on Ruby (i've been a Watir user
before) and I was wondering if someone could give me quick example of
how to use a regular expression to select an element.

For example, instead of matching an exact id like this

@selenium.click "Login"

I want to make it more flexible and use regular expression, i tried
playing around with this


@selenium.click /Login/ # ruby way
@selenium.click "regexp:Login" #selenium way
@selenium.click "*Login*" # random guess

And neither work.. I tried finding documentation on this and couldn't
find it. I'm pretty sure it's something simple that i'm missing.. Can
someone help me out?

Thanks,
Steve

wade

unread,
Nov 12, 2010, 1:33:51 PM11/12/10
to Selenium Users
While it might not be exactly what you're looking for, you can always
use the XPath contains() function for partial matching of an attribute
value:

@selenium.click "//input[contains(@id, 'Login')]"

Of course it's not nearly as versatile as a regular expression, but it
can come in handy nonetheless.


wade

Ross Patterson

unread,
Nov 12, 2010, 1:50:09 PM11/12/10
to seleniu...@googlegroups.com
If you want to use a regular expression, the correct locator syntax is "link=regexp:the_expression", where "the_expression" is a regular expression in the JavaScript format. There are other alternatives as well, see the "String-match Patterns" documentation at http://release.seleniumhq.org/selenium-core/1.0.1/reference.html#patterns for the full details.

For example:

# Click on a link defined like '<A HREF='http://wherever'>blah blah Login blah blah</A>'
@selenium.click "link=regexp:.*Login.*"

Ross

Steve Tangsombatvisit

unread,
Nov 12, 2010, 2:51:22 PM11/12/10
to seleniu...@googlegroups.com
Hi ross, that works perfectly. Thanks

Steve

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




--
---------------------------------------------------
Steve B Tang
My website - http://sbtang.blogspot.com/
Reply all
Reply to author
Forward
0 new messages