views:

3278

answers:

3

I'm using selenium RC and I would like, for example, to get all the links elements with attribute href that match:

http://[^/]*\d+com

I would like to use:

sel.get_attribute( '//a[regx:match(@href, "http://[^/]*\d+.com")]/@name' )

which would return a list of the name attribute of all the links that match the regex. (or something like it)

thanks

A: 

You can use the Selenium command getAllLinks to get an array of the ids of links on the page, which you could then loop through and check the href using the getAttribute, which takes the locator followed by an @ and the attribute name. For example in Java this might be:

String[] allLinks = session().getAllLinks();
List<String> matchingLinks = new ArrayList<String>();

for (String linkId : allLinks) {
    String linkHref = selenium.getAttribute("id=" + linkId + "@href");
    if (linkHref.matches("http://[^/]*\\d+.com")) {
        matchingLinks.add(link);
    }
}
Dave Hunt
I don't think that's what he wanted - he wants to find an element using a regex as the locator (as part of the XPATH)
noam
The question mentions getting *all* links that match the regex. As Selenium doesn't support this (to my knowledge), getting all links from the page and then using your client language to check the locations against a regular expression is a sensible solution.
Dave Hunt
I've edited my example code to do a regular expression match. I didn't do this originally because it depends on the client language in use, and wanted to keep the answer simple.
Dave Hunt
There's no way to use regular expressions in the locators. There's some stuff that can be done, like using the contains() function in xpath. Anyway, for regexp, I think this is the best alternative. +1
Santi
+1  A: 

The answer above is probably the right way to find ALL of the links that match a regex, but I thought it'd also be helpful to answer the other part of the question, how to use regex in Xpath locators. You need to use the regex matches() function, like this:

xpath=//div[matches(@id,'che.*boxes')]

(this, of course, would click the div with 'id=checkboxes', or 'id=cheANYTHINGHEREboxes')

Be aware, though, that the matches function is not supported by all native browser implementations of Xpath (most conspicuously, using this in FF3 will throw an error: invalid xpath[2]).

If you have trouble with your particular browser (as I did with FF3), try using Selenium's allowNativeXpath("false") to switch over to the JavaScript Xpath interpreter. It'll be slower, but it does seem to work with more Xpath functions, including 'matches' and 'ends-with'. :)

Chris Jaynes
how do you check your xpath? I usually use firefox's add on xpath-checker. But it doesn't recognize the regex in the xpath.
Guy
Using that xpath-checker add-on is a great idea! I never thought to look for one.I don't have write too many xpath locators, though. At my job, I built a tool-independent test framework that builds locators for multiple tools, including Selenium, using our own simple syntax.I only had to learn these xpath locators well enough to write some code that could generate them. :)
Chris Jaynes
A: 

A possible solution is to use sel.get_eval() and write a JS script that returns a list of the links. something like the following answer: http://stackoverflow.com/questions/2007367/selenium-is-it-possible-to-use-the-regexp-in-selenium-locators/2007531#2007531

Guy