使用 selenium webdriver/java 从文本中提取数字

Extracting a number from a text with selenium webdriver/java

我正在测试显示以下消息的网页:

"You still have X free messages out of 100"

其中 x 可以是任何小于 100 的数字。用户每发送一条消息,该数字就会减 1。

我正在使用带有 java 的 Selenium webdriver 来自动执行测试,我需要知道如何从该文本中提取 x 并将其转换为整数,以便我可以将它与数字进行比较。

how I can extract the x from that text and turn it into an integer?

尝试 split() 并从字符串中获取第三个元素。

int x=Integer.parseInt("You still have X free messages out of 100".split(" ")[3]);

使用下面的代码。根据您的方便定位元素并将其替换为第一行代码

 String OSNAMES= Driver.findElement(By.xpath("YOUR XPATH")).getAttribute("value");
 String[] parts = OSNAMES.split(" ");
 String OS = parts[3];
 int a=Integer.parseInt(OS);
 System.out.println(a);