'Selenide - check if link text matches to regular expression
Is it possible to find a link that matches to the regular expression?
Here https://demoqa.com/links the 2nd link has a random name that is generated each time with the pattern Home[a-zA-Z]{5}.
Is there any way to check if link text matches to the RegEx or simply contains substring + smth? XPath //a[contains(text(),'substring')] or $(partialLinkText("substring")) do not fit because they will select both Home and Home[a-zA-Z]{5}
Solution 1:[1]
First the regex should include numbers as well: "Home[a-zA-Z\d]{5}", and the only solution I can thing of (at least for now), is to iterate over all link elements that contains Home:
StreamSupport.stream(Selenide.$$(Selectors.byPartialLinkText("Home")).asFixedIterable().spliterator(), false)
.filter( element -> element.getText().matches("Home[a-zA-Z\\d]{5}") )
.findFirst();
But if you know that your browser supports XPath 2.0, so you can use matches function with regex:
Selenide.$(Selectors.byXpath("//a[matches(text(), 'Home[a-zA-Z\\d]{5}')]"));
Maybe worth nothing the following links about XPath 2.0 support in browsers:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | O.Badr |
