如何从文本中验证编号的脚本?例如:我喜欢从字段“Staus(2)”中获取数值“2”

问题描述 投票:-1回答:2

在Selenium上,我正在编写脚本以从文本中获取数字。假设有一个字段'Sta​​tus(2)'。括号中的数字不断变化。我想得到价值。

selenium-webdriver
2个回答
0
投票

此代码应该返回您提供的元素的文本:

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

希望这可以帮助!


0
投票

这是解决方案。

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);
© www.soinside.com 2019 - 2024. All rights reserved.