0

I'm trying to use regex to match an id containing an unpredictable number in the middle of an otherwise predictable string, an example:

<div id="type-84289-model" class="vehicle">

I've tried various things, but it seems like the most obvious that should work is:

By.xpath("//div[matches(@id, 'type-.+-model')]"));

However that doesn't find the element. Can anyone point me in the right direction.

5
  • 1
    Is it XPath 2? I think that XPath 1 doesn't support regular expressions. Commented Aug 28, 2014 at 20:38
  • In case @curiosu is right you may try [starts-with(@id, 'type-') and ends-with(@id, '-model')]. Commented Aug 28, 2014 at 20:44
  • I believe it supports Xpath 2. From what I understand of Selenium it boils down to whatever the browser you're testing supports and I've been using Firefox 31.0. However, I did try using 'starts-with' and 'ends-with' as @Ciapan suggest, but no luck... I'm getting an InvalidSelectorException exception. Commented Aug 28, 2014 at 21:15
  • I'm surprised at the implication that Firefox supports XPath 2.0. Do you have a reference for that? Commented Aug 28, 2014 at 21:26
  • My assumption about Firefox supporting XPath 2 appears to be wrong. Some Googling suggests that Firefox does not indeed support 2.0. Commented Aug 28, 2014 at 21:59

4 Answers 4

2

You can find the element, using following xpath:

driver.findElement(By.Xpath("//div[contains(@id, 'type-')][contains(@id, '-model')][@class='vehicle']"))
Sign up to request clarification or add additional context in comments.

1 Comment

I would at least change the first contains() to starts-with(). It's just as readable, less likely to generate false positives, and probably faster.
1

curiosu is right that XPath 1 doesn't support regexps, and moreover Selenium doesn't support XPath 2. :-(

As you pointed out, ends-with() doesn't exist in XPath 1.0. So we can adapt CiaPan's answer as follows:

By.xpath("//div[starts-with(@id, 'type-') and
   substring(@id, string-length(@id)-6) = '-model']"));

1 Comment

This does work to match my div id's, but I marked @German Petrov's as the answer since it will be a bit easier to read in the code and there are a lot of div's to match. Thanks for your help, this was definitely a good learning experience with selenium and xpath!
0

In case the id always start with a string ending with a hyphen followed by a number and ending with a string starting with a hyphen this could work:

//div[ contains (translate(@id, '1234567890',''),'--')]

so in your case

By.xpath("//div[ contains (translate(@id, '1234567890',''),'--')]");

Comments

0

Do you seriously want to use xpath and xpath to locate element? If not, and you just want to locate div, then you can use next selector:

driver.findElement(By.Css(".vehicle"));
//or
driver.findElement(By.Css("div[class='vehicle']"));

UDPATE If you need to find div that matches pattern type-...-model you still can use css selector for that. But AFAIK css selectors doesn't support regexp, so you can use starts/ends with attributes: Smth like this:

//find all divs which id starts with type
driver.findElement(By.Css("div[class^='type']"))
//find all divs which id ends with model
driver.findElement(By.Css("div[class$='model']"))
//find all divs which id starts with type and ends with model
driver.findElement(By.Css("div[class^='type'][class$='model']"))

Should work now.

1 Comment

He needs to find a div whose id matches type-...-model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.