2017-03-14 32 views
0

下面是我的表格結構,我使用Selenium使用Java遍歷所有行並捕獲數字「00955222222222」,它將在任意單元格中隨機顯示。一旦我得到這個,我已經讀完整行並獲得授權代碼。我可以實現直到循環行和列。但需要輸入我如何獲得授權碼?請幫助Java:如何迭代表格單元以隨機更改表格捕獲數字

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

+0

找到表格並使用「標記名」獲取「tr」列表。遍歷「tr」列表並獲取「td」列表。遍歷「td」並獲得認證碼。如果您只需要驗證碼,請提供一個條件。這是你想要的? – shank087

回答

0

我喜歡這樣的事情寫功能,因爲它們很可能被重複使用。下面的功能需要您正在搜索的手機號碼並返回授權碼。基本邏輯是抓住TR並通過它們循環尋找包含移動號碼的那個。一旦你發現TR,使用正則表達式從TR中找到認證碼並返回。

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"; 
} 
+0

好的,JeffC,甚至我已經試過一些示例代碼會複製到這裏。我也會嘗試這個。 – Aditya

+0

我嘗試了上面的代碼,但它總是返回舊的認證碼,但不是最近的代碼。有多個標籤具有相同的號碼,我需要返回最新的 – Aditya

+0

「最新的」是什麼意思? – JeffC

相關問題