2

I have the following WebElements:

<input id="" type="checkbox" value="/p1/foobar1" name="checkbox1" partition="p1" onclick=""/>
<input id="" type="checkbox" value="/p1/foobar" name="checkbox2" partition="p1" onclick=""/>
<input id="" type="checkbox" value="/p2/foobar2" name="checkbox3" partition="p2" onclick=""/>

And I'm wanting to find the 2nd element in the above source (the one with with a value of /p1/foobar) based on value attribute using regex in an xpath. I have tried the following:

driver.findElement(By.xpath("//input[matches(@value,'.*\\/foobar$')]"));

But it throws an InvalidSelectorException. I've gotten it to work with cssSelector like so ...

driver.findElement(By.cssSelector(input[value$='\\/foobar']));

But I'm still curious how to accomplish this with xpath. Any suggestions?

EDIT: I should also note that I want to be able to find this element without knowing the characters before the last slash (aka /p1/)

user2150250
  • 4,797
  • 11
  • 36
  • 44

3 Answers3

4

Try

//input[@value='/p1/foobar']

If you don't know what it starts with, you have to resort to functions:

//input[contains(@value, 'foobar') and substring-after(@value, 'foobar') = '']

Here is a 'fiddle'

<?xml version="1.0" encoding="UTF-8"?>
<result>
<input id="" name="checkbox1" onclick="" partition="p1" type="checkbox" value="/p1/foobar1"/>
<input id="" name="checkbox2" onclick="" partition="p1" type="checkbox" value="/p1/foobar"/>
<input id="" name="checkbox3" onclick="" partition="p2" type="checkbox" value="/p2/foobar2"/>
</result>

http://www.xpathtester.com/xpath/4dc56e27ca1674ea0b5b48357214b907

As noted in Can I use a Regex in an XPath expression?, XPath 1.0 doesn't support regular expressions.

Here are many examples:

https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

Here is a list of XPath functions:

https://msdn.microsoft.com/en-us/library/ms256180(v=vs.110).aspx

Community
  • 1
  • 1
Chloe
  • 25,162
  • 40
  • 190
  • 357
  • Thanks for the response but please see my edit. I'm trying to accomplish finding this element without knowledge of the characters before the last slash. – user2150250 May 13 '15 at 17:47
0

Use xpath contains function. Fairly complicated. Probably you do not want to go this route. But this can be done also

//input[contains(@value,'foo') and (not(contains(@value,'foobar1'))) and (not(contains(@value,'foobar2')))]

*Keep the complexity in mind, I think the CssSelector is the best fit for this scenario.

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • That won't work because the other element attributes also contain `foobar`. – Chloe May 13 '15 at 17:40
  • Chloe is correct. This will find the first webelement since it also contains `foobar`, not the second. – user2150250 May 13 '15 at 17:48
  • @Saifur I'm really trying to use `matches` and regex to accomplish this webelement find. It should not depend on knowledge of all the other source. – user2150250 May 13 '15 at 17:50
  • @user2150250 I am afraid as per my knowledge Selenium only supports xpath version 1 which does not support extensive regex search – Saifur May 13 '15 at 17:52
0

XPath does not support regex. You will have to resort to other XPath functions, for your example you can use something like:

//input[ends-with(@value,'foobar')]

There is a nice list of XPath functions here.

SiKing
  • 10,003
  • 10
  • 39
  • 90