chandrashekar chandrashekar - 2 months ago 6
Java Question

How to identify if brackets in the element are present or not using Java and Selenium WebDriver

I need to write a code to identify if brackets are present or not in a given element in

Selenium-Webdriver
.

For example : the web element contain a number like
(235)
within the brackets. Then I need to check the brackets are present in that particular web element or not.

Answer

Better way to use Regex to determine element text present with round brackets () as below :-

String regex = "\\(\\w+\\)"; //it will match any word present inside round brackets 

or

String regex = "\\(\\d+\\)"; //it will match only digits present inside round brackets 

Use anyone of the regex expression to determine element text present inside round brackets () as:-

WebElement el = driver.findElement(..);
String text = el.getText();

if(text.matches(regex)) {
    //do your further stuff
}