views:

138

answers:

2

I have problems running the matches XPATH method:

@Test(groups = "regression")
@Parameters( { "baseUri", "locale" })
public void testShopping(String baseUri, String locale) throws Exception{
    session().allowNativeXpath("false");
    String shoppingLink = session().getAttribute("//div[@id='footernav_links']//a[matches(@href,'.*shopping.*')]/@href");
    LOGGER.info(shoppingLink);
}

throws excepotion

1:25.871 INFO - Command request: getAttribute[//div[@id='footernav_links']//a[matches(@href,'.*shopping.*')]/@href, ] on session 057af825ff224b16877a61b97b974b72 13:31:25.912 INFO - Got result: ERROR: Element //div[@id='footernav_links']//a[matches(@href,'.*shopping.*')]/ not found on session 057af825ff224b16877a61b97b974b72 13:3

I am using FF3.6 and got the motivation for this code from the article

how-to-use-regex-in-selenium-locators

+1  A: 

Got it to work. I now use the following syntax

session().allowNativeXpath("false");
String shoppingLink = session().getAttribute(
    "//div[@id='footernav_links']//a[matches(@href,'/shopping/')]/@href");
LOGGER.info(shoppingLink);

the Link I am searching for is of the form
http://mysite.com/wcs/shopping/overview?cc=gb&lc=en
I am not sure if this is completely correct though.

Anadi Misra
A: 

You could use contains as follows:

xpath=id('footernav_links')//a[contains(@href, '/shopping/')]@href

Also, the / before the final @ shouldn't be necessary, and you can simplify //div[@id='footernav_links'] to id('footernav_links') as shown above. Note that you will need the xpath= prefix if you do this though.


Alternatively, you could use a CSS locator:

css=#footernav_links a[href*=/shopping/]@href
Dave Hunt
Thanks ...... this syntax is more compact and I can re-use the expression thru string substituion too. Saved some lines of code :-)
Anadi Misra