Reputation: 692
I use Selenium 2.35.0, latest Firefox, and Linux OS.
I unable to use regular expression in my java application.
I have a line like this:
Iterator<WebElement> iterator =
driver.findElements(By.xpath("//a[matches(@href, 'site.ru/[0-9]*/')]")).iterator();
Exception occured:
org.openqa.selenium.InvalidSelectorException: The given selector //a[matches(@href, "site.ru/[0-9]/")] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //a[matches(@href, "site.ru/[0-9]/")]
Could you please help me with investigation reason of this fault?
Upvotes: 0
Views: 5736
Reputation: 111491
It'd be better to see your real input, or a portion of which that shows the problem, but perhaps this'll help.
//a[matches(@href, 'site.ru/[0-9]*/')]
against these links:
<html>
<div>
<a href="site.ru//">Link 1</a>
<a href="site.ru/123/">Link 2</a>
<a href="http://site.ru/123/">Link 3</a>
<a href="site.ru/">Link 4</a>
<a href="site.ru/123">Link 5</a>
<a href="site.ru/abc/123">Link 6</a>
</div>
</html>
will match Links 1-3 but not 4-6.
If you need to match 4-6 too, you could drop the trailing slash in the regex:
//a[matches(@href, 'site.ru/[0-9]*')]
If this is too liberal and picks up cases you do not want to match, then comment below on which specific cases must be excluded, and we'll try to adjust accordingly.
Upvotes: 1