【发布时间】:2017-09-13 03:26:33
【问题描述】:
我有下表,如果它与手机号码“00955555555555”匹配并且需要获取代码“89721”,我正在读取整行。
table border="2" cellspacing="0" width="100%" cellpadding="0">
手机号码 日期 消息
<td align="left" nowrap><font face="times new roman" size=3 > 00955555555555</font></td>
<td align="left" nowrap><font face="times new roman" size=3 > 2017-04-17 17:34:06.72</font></td>
<td align="left"><font face="times new roman" size=3 > Your authentication code is 89721 to add name as beneficiary for payment from your account. If you have not requested to add this beneficiary, please contact the bank
立即拨打 00971 600 54 0000。
<td align="left" nowrap><font face="times new roman" size=3 > 955111111111</font></td>
<td align="left" nowrap><font face="times new roman" size=3 > 2017-04-17 17:31:13.893</font></td>
<td align="left"><font face="times new roman" size=3 > Your authentication code is: 91518. Please do not share this code with any person.</font></td>
<tr>
我尝试了下面的代码,但它返回的是手机号码,而不是五位数的代码。
代码:
public String entire_row_is_read_which_matches_with_the_Mobile_number() throws Throwable {
String mobilenumber="00955555555555";
//Date validate = null;
{
List<WebElement> rows = driver1.findElements(By.cssSelector("tr"));
for (WebElement row : rows)
{
String text = row.getText();
if (text.contains(mobilenumber))
{
String regex = " (\\d+)"; //Your authentication code is
System.out.println(regex);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
if (matcher.find())
{
valueis = matcher.group(1);
System.out.println(valueis);
break;
}
【问题讨论】:
-
不要使用正则表达式解析html。
-
然后呢?我如何获得它
-
@vallentin 他不是。他正在使用 Selenium 并尝试解析没有 HTML 标记的结果文本
-
这是什么数据?这不是第一次被问到这个问题。 stackoverflow.com/questions/42787481/…,你的代码是我的回答,稍作改动。
标签: java html regex selenium-webdriver