0
<div class="order-number">
<h3>"Order number: "<strong>123-123123</strong> </h3>
</div>

Any idea how to select the order number? I use Selenium 2. I tried this:

driver.findElement(By.xpath(".//*[matches(text(),'\\d+-\\d+']"));

But it's not working. Does Xpath2 support regex?

The number of the order is always different, but the style of XXX-XXXXXX is always the same.

2 Answers 2

1

A different approach would be to search by CSS selector instead:

By.cssSelector(".order-number H3 STRONG")

It's a bit more frail if the page structure changes though.

A better solution (if you are able to change the page code) is to put an ID on the <strong> tag and use By.id. That's much quicker, less frail, and more readable than XPaths or CSS-Selectors.

Sign up to request clarification or add additional context in comments.

Comments

1

Simply try

driver.findElement(By.xpath("//div[@class='order-number']//strong")).getText();

Also note that selenium supports matches() in XPath. The cause of your issue seem to be missing closing parenthesis:

now

matches(text(),'\\d+-\\d+' 

should be

matches(text(),'\\d+-\\d+')

working one

//h3/strong[matches(text(),'\d+-\d+')]

1 Comment

Neither way is working for me, always the same result - The string '//h3/strong[matches(text(),'\d+-\d+')]' is not a valid XPath expression.

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.