Adithya
Adithya

Reputation: 1857

How to get the last row number of table which is dynamically updating?

How we can get the Table(in a webpage) row using the value it contain.

Suppose a table of 3*3. In that cell(2,3) cell text is "Test". So how to find the row number = 2 using the the cell text "Test".

Selenium-2.53.1 Java-8

This is the xpath

//table[2]/tbody/tr/td[2]/table[2]/tbody/tr[7]/td/div[2]/table/tbody/tr[1]/td[1]/div/a/font

in which bolded tr (here tr[1]) is the row I want to change dynamically & the text is inside the bolded td (here td[1])

My html code is :

<html>
<head></head>
<body>
<form name="fm" method="post">
 <table>
    <!-- --Some data--- -->
 </table> 

  <table width="100%" height="100%" border="0" >
  <tr valign="top" > 
  <td width="200" height="100%" rowspan="3"  align="center" > 
        <p></p>
        <table width="89%" height="270" border="0">
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <!-- --Some data--- -->
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr> 
       <td><table width="100%" height="71" border="0" align="right">
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
       </table></td>
    </tr>

    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <!-- --Some data--- -->
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    </table>
    <p></p>
  </td>  
  <td width="622" height="30" colspan="3" align="left" valign="top">
    <table width="100%" height="40" align="center">
          <tr><!-- --Some data--- --></tr>
    </table>
        <table width="476" height="110" align="center">
    <tr></tr>
    <tr></tr>
    <tr></tr>  
    <!-- --Some data--- -->
    <tr></tr>     
    <tr></tr>
    <tr></tr>
    <tr> 
       <td height="22" colspan="3">
          <div align="left"> 
        <table width="469" border="1">
                  <tr bgcolor="#006291"> 
                     <td width="81" bgcolor="#006291"><div align="center">SlNo.</div></td>
                     <td width="82" bgcolor="#006291"><div align="center">Code</div></td>
                     <td width="300" bgcolor="#006291"><div align="center">Name</div></td>
          </tr>
                </table>
              </div>
              <div align="left" style="width:465px;Height:150px;"> 
        <table width="465" bgcolor="#FFFFFF" border="1">
        <tr bordercolor="#006291" bgcolor='#FFEADF' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >1</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >01</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >tvm1</font></a></div></td>
        </tr>
        <tr bordercolor="#006291" bgcolor='#F1F8FC' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >2</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >02</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >tvm2</font></a></div></td>
        </tr>
        <tr bordercolor="#006291" bgcolor='#FFEADF' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >3</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >03</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >tvm3</font></a></div></td>
        </tr>
        <tr bordercolor="#006291" bgcolor='#F1F8FC' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >4</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >04</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >tvm4</font></a></div></td>
        </tr>
        <tr bordercolor="#006291" bgcolor='#FFEADF' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >5</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >10</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >tvm5</font></a></div></td>
        </tr>                
        </table>
              </div>
       </td>
    </tr>
    </table>

  </td>
  </tr>
  </table>
</form>
</body>
</html>

Upvotes: 0

Views: 2786

Answers (3)

prashanth
prashanth

Reputation: 66

I tried capturing the column data of all rows into a List(observe the xpath of 'rowList' below, no number given for the final 'tr' which gets the column data for all rows), and using foreach got the index of the row which contains required text.

Will have to change the value in 'expRowValue' to have it worked with different data.

public class Table {
WebDriver driver; 

By rowList = By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[3]/td/table/tbody/tr/td[2]");

  @Test
  public void f() {
  String expRowValue="$513";

System.setProperty("webdriver.chrome.driver", "C:/Users/dell/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/");
driver.manage().window().maximize();

List<WebElement>rowsData =  driver.findElements(rowList);
int k=1;    
for (WebElement rowValue: rowsData) {
    if (rowValue.getText().equals(expRowValue)){
        System.out.println("ROw Index::" + k);
    }
k++;    
}

}
}

Upvotes: 0

Saurabh Gaur
Saurabh Gaur

Reputation: 23825

If you want to click on <td> on the basis of text try below xpath :-

.//td[child::a/font[text() = 'your text hare']]

Or if you to click on <a> inside <td> on the basis of text try below xpath :-

.//td/a[child::font[text() = 'your text hare']]

Or it you want to click on table cell using their row and column index follow this link

Upvotes: 0

Grasshopper
Grasshopper

Reputation: 9058

"count(//table[@id='tableid']//td[.='Test']/parent::tr/preceding-sibling::tr) + 1"

put in your own table identifier... depends on it being a simple table without rowspans and colspans.

Upvotes: 1

Related Questions