Java:如何遍历表格单元格以在随机更改的表格中捕获数字

时间:2017-03-14 13:34:28

标签: java selenium-webdriver

下面是我的表结构,我使用Selenium使用Java迭代所有行并捕获数字“00955222222222”,它将在任何单元格中随机显示。一旦我得到这个,我已经阅读了整行并获得了授权代码。我可以实现直到循环行和列。但是需要输入如何获得身份验证代码?请帮忙

<table border="2" cellspacing="0" width="100%" cellpadding="0">
  <tr bgcolor="#CCCCCC">
    <td align="left">
      <font face="times new roman" size=3><b>&nbsp;Mobile Number</b></font>
    </td>
    <td align="left">
      <font face="times new roman" size=3><b>&nbsp;Date</b></font>
    </td>
    <td align="left">
      <font face="times new roman" size=3><b>&nbsp;Message</b></font>
    </td>
  </tr>
  <tr bgcolor="#EBD8B8">
    <td align="left" nowrap>
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;955111111111</font>
    </td>
    <td align="left" nowrap>
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;2017-03-14 16:57:20.027</font>
    </td>
    <td align="left">
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;Your authentication code is: 11292 to transfer INR 5,500.00 through online/mobile banking. If you have not requested any transfer, please contact the bank IMMEDIATELY on 00971 600 54 0000 .</font>
    </td>
  </tr>
  <tr bgcolor="#FFFFFF">
    <td align="left" nowrap>
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;00955222222222</font>
    </td>
    <td align="left" nowrap>
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;2017-03-14 16:55:30.187</font>
    </td>
    <td align="left">
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;Your authentication code is: 59859 to pay AED 1.00 to Empower through online/mobile banking. Please do not share this code with any person. If you have not requested any payment, please contact the bank IMMEDIATELY on 00971 600
        54 0000 .</font>
    </td>
  </tr>
  <tr bgcolor="#EBD8B8">
    <td align="left" nowrap>
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;00955111111111</font>
    </td>
    <td align="left" nowrap>
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;2017-03-14 16:54:46.25</font>
    </td>
    <td align="left">
      <font face="times new roman" size=3>&nbsp;&nbsp;&nbsp;Your authentication code is: 79404 to transfer PKR 7000 through online/mobile banking. If you have not requested any transfer, please contact the bank IMMEDIATELY on 00971 600 54 0000 .</font>
    </td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

我喜欢为这样的事情编写函数,因为它们很可能被重用。下面的函数接收您要搜索的手机号码并返回授权码。基本逻辑是抓住TR并循环遍历它们,寻找包含移动号码的TR。一旦找到TR,使用正则表达式从该TR中查找auth代码并将其返回。

public static String GetAuthCode(String mobileNumber)
{
    List<WebElement> rows = driver.findElements(By.cssSelector("tr"));
    for (WebElement row : rows)
    {
        String text = row.getText();
        if (text.contains(mobileNumber))
        {
            String regex = "Your authentication code is: (\\d+)";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);

            if (matcher.find())
            {
                return matcher.group(1);
            }
        }
    }

    return "not found";
}