在Selenium上,我正在编写脚本来从文本中获取数字。 假设有一个字段"状态(2)"。括号中的数字不断变化。我想获取值。
此代码应返回您提供的元素的文本:
WebElement web_element_found = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects"));
String element_text = web_element_found.getText();
然后你可以看看这个答案,了解如何使用正则表达式从字符串中提取数字:正则表达式从 Java 中的字符串中提取数字
希望这有帮助!
这是解决方案。
String rawText = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects")).getText();
String number = rawText.substring(s.indexOf("(") + 1).substring(0, s.indexOf(")"));
System.out.println(number);