简体   繁体   English

使用Selenium Webdriver / java从文本中提取数字

[英]Extracting a number from a text with selenium webdriver/java

I am testing a webpage where the following message is displayed: 我正在测试一个显示以下消息的网页:

"You still have X free messages out of 100" “您仍然有100条中有X条免费消息”

where x can be any number smaller than 100. And every time the user sends a message, that number decreases by 1. 其中x可以是小于100的任何数字。每当用户发送一条消息时,该数字就会减少1。

I am using Selenium webdriver with java for automating the tests and I need to know how I can extract the x from that text and turn it into an integer, so I can compare it with a number. 我正在使用带有Java的Selenium Webdriver来自动化测试,我需要知道如何从该文本中提取x并将其转换为整数,以便可以将其与数字进行比较。

Use below code. 使用下面的代码。 locate element according to your convenience and replace it with first line of code 根据您的方便定位元素,并将其替换为第一行代码

 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);

how I can extract the x from that text and turn it into an integer? 如何从该文本中提取x并将其转换为整数?

try split() and get 3rd element from the string. 尝试split()并从字符串中获取第3个元素。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM