chandrashekar chandrashekar - 1 year ago 46
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


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

Answer Source

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 


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