简体   繁体   English

如何使用Java和Selenium WebDriver识别元素中的方括号是否存在

[英]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.

相关问题 使用带有 Java 的 Selenium WebDriver 检查元素不存在的最佳方法 - Best way to check that element is not present using Selenium WebDriver with java 如何使用 Selenium WebDriver 验证元素中是否存在属性? - How to verify an attribute is present in an element using Selenium WebDriver? 如何使用 Selenium 和 Java 识别元素 - How to identify the element using Selenium and Java 如何使用Java和Selenium WebDriver识别动态WebElement上传图片 - How to identify dynamic WebElement to upload the picture using Java and Selenium WebDriver 使用 Selenium WebDriver 测试元素是否存在 - Test if an element is present using Selenium WebDriver Selenium Webdriver识别按钮并使用Java单击它 - Selenium webdriver identify button and clicking on it using Java 使用 Selenium WebDriver 和 java 断言 WebElement 不存在 - Assert that a WebElement is not present using Selenium WebDriver with java 使用Java检查Selenium WebDriver在页面上是否存在或显示元素的正确方法是什么? - Which is the correct way to check if an element is present or displayed on a page with Selenium WebDriver using Java? 如何验证硒2中存在或可见的元素(Selenium WebDriver) - How to verify element present or visible in selenium 2 (Selenium WebDriver) 如何使用 Selenium WebDriver 使用 Java 确定元素的颜色? - How to determine the color of an element using Java using Selenium WebDriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM