0

I have below table where I am reading entire row if it matches the mobile number "00955555555555" and need to get the code "89721".

table border="2" cellspacing="0" width="100%" cellpadding="0">

 Mobile Number  Date  Message

<td align="left" nowrap><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;00955555555555</font></td>
<td align="left" nowrap><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;2017-04-17 17:34:06.72</font></td>
<td align="left"><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;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 

IMMEDIATELY on 00971 600 54 0000.

<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-04-17 17:31:13.893</font></td>
<td align="left"><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;Your authentication code is: 91518. Please do not share this code with any person.</font></td>
<tr>

I have tried below code but it returns the mobile number but not the five digit code.

Code :

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;

                         }
Sowmya
  • 47
  • 1
  • 6
  • 2
    Don't use regex to parse html. – vallentin Apr 17 '17 at 13:45
  • Then ? How do I get it – Sowmya Apr 17 '17 at 13:49
  • Use this as a reference. http://stackoverflow.com/a/2170950/2792713 – xvf Apr 17 '17 at 13:59
  • @vallentin he isn't. He is using Selenium and trying to parse the resulting text without HTML tags – Strelok Apr 17 '17 at 14:58
  • What is this data? This is not the first time this question has been asked. http://stackoverflow.com/questions/42787481/java-how-to-iterate-through-table-cells-to-capture-number-in-randomly-changing and your code is my answer with minor changes. – JeffC Apr 17 '17 at 20:48

2 Answers2

2

I like writing functions for things like this since they are likely to be reused. The function below takes in the mobile number that you are searching for and returns the auth code.

public String GetAuthCode(String number)
{
    String code = driver
            .findElement(
                    By.xpath("//tr/td[contains(.,'" + number + "')]/following-sibling::td[contains(.,'Your authentication code')]"))
            .getText();
    String regex = "Your authentication code is: (\\d+)";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(code);

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

    return "";
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Thanks it worked. The same one I have asked but there was difference in the table earlier I was working on. – Sowmya Apr 18 '17 at 06:03
-1

you can use the jsoup.jar to get the datas you want . https://jsoup.org/

demo:

    String html = " <table border=\"2\" cellspacing=\"0\" width=\"100%\" cellpadding=\"0\">" + "<tr>"
            + "<td align=\"left\" nowrap><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;00955555555555</font></td>"
            + "<td align=\"left\" nowrap><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;2017-04-17 17:34:06.72</font></td>"
            + "<td align=\"left\"><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;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</td>"
            + "</tr>" + "<tr>"
            + "<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-04-17 17:31:13.893</font></td>"
            + "<td align=\"left\"><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;Your authentication code is: 91518. Please do not share this code with any person.</font></td>"
            + "</tr>" + "</table>";

    Document doc = Jsoup.parse(html);
    String text = doc.select("tr >td:nth-child(2n+1)").text();
    Matcher m = Pattern.compile("\\d+").matcher(text);
    List<String> result = new ArrayList<String>();
    while (m.find()) {
        result.add(m.group());
    }
    System.out.println(result);

output:

[00955555555555, 89721, 955111111111, 91518]
Mr.lin
  • 99
  • 3