在Selenium上,我正在编写脚本以从文本中获取数字。假设有一个字段'Status(2)'。括号中的数字不断变化。我想得到价值。
此代码应该返回您提供的元素的文本:
WebElement web_element_found = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects"));
String element_text = web_element_found.getText();
那么你可以看看这个答案,了解如何使用正则表达式从字符串中提取数字:Regex to extract digit from string in 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);