[英]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
. 我需要编写代码来识别
Selenium-Webdriver
的给定元素中是否存在括号。
For example : the web element contain a number like (235)
within the brackets. 例如:web元素在方括号内包含一个数字
(235)
。 Then I need to check the brackets are present in that particular web element or not. 然后,我需要检查括号是否存在于特定的Web元素中。
WebElement element = driver.findElement(By.Id("your_id_here)")); //or any other locating method
if(element.getText().contains("[") || element.getText().contains("]")) {
//do your stuff
}
Note: use ||
注意:使用
||
if only one of the opening or closing bracket should be present. 如果只应有一个打开或关闭支架。 If both of them are expected, use
&&
. 如果两者都预期,请使用
&&
。
Better way to use Regex
to determine element text present with round brackets ()
as below :- 使用
Regex
确定带有圆括号()
元素文本的更好方法如下:
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:- 使用任何
regex
表达式来确定圆括号()
元素文本,如下所示:-
WebElement el = driver.findElement(..);
String text = el.getText();
if(text.matches(regex)) {
//do your further stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.