The structure of the Html I need to work with is shown in the screenshots below.

Image 1

Image 2

I need to get the values of the first and third column of each data row in the table. There is nothing unique about the rows or cells.

I've tried:

 //String CPONEW="//table[@id='GridView1']/tbody/tr[2]['@id!= or @class=!"+j+"']/td[1]"; 

When I try to use this xpath I only get the first tr[2]/td[1] val but I need to get each tr[2] to tr[12] / td[1] and td[3] values

  • 2
    What did you try? Can you post your code as of now? And it is always better to enter the code itself than posting a screenshot of the code. Doesn't allow others to take the code into an editor and experiment. – Dakshinamurthy Karra Sep 25 '18 at 11:33
  • i have used the Xpath for finding the tr rows in the above screen shoot and teh xpath is List<WebElement> allExpandRows = c_WebDriver.findElements(By.xpath("//table[@id='GridView1']/tbody/tr[not(@id) or not(@class)]")); – Rahul Sangareddy Sep 25 '18 at 11:36
  • i have got 12 rows that is correct. But i need to get the values tr[2]/td[1]&td[2] of each row – Rahul Sangareddy Sep 25 '18 at 11:39
  • Use findElement relative to a webelement to get the values. Still did not understand what issue you have :( – Dakshinamurthy Karra Sep 25 '18 at 11:47
  • i need the rows from tr[2] to tr[12] in that tr we have td also so i need that td[1] and td[3] values – Rahul Sangareddy Sep 25 '18 at 12:15

Try the below code:

List <WebElement> tableRows = table.findElements(By.xpath("tr"));

List <WebElement> tableCells= table.findElements(By.xpath("tr/td"));



//you can assign to String 

String FirstColumn=tableCells.get(1);


You can filter out the first row based on its style, color property then match for 1st and 3rd column by using the xpath position function. The corresponding XPath expression would be:

//table[@id='GridView1']/tbody/tr[contains(@style,'color:#333333')]/td[position()=1 or position()=3]

With the following test input, you can test it here.

<table id="GridView1">
    <tr style="color:White"><td>header1</td><td>header2</td><td>header3</td><td>header4</td></tr>
    <tr style="color:#333333"><td>data1</td><td>data2</td><td>data3</td><td>data4</td></tr>
    <tr style="color:#333333"><td>data12</td><td>data22</td><td>data32</td><td>data42</td></tr>
    <tr style="color:#333333"><td>data13</td><td>data23</td><td>data33</td><td>data43</td></tr>
    <tr style="color:#333333"><td>data14</td><td>data24</td><td>data34</td><td>data44</td></tr>
    <tr style="color:#333333"><td>data15</td><td>data25</td><td>data35</td><td>data45</td></tr>
    <tr style="color:#333333"><td>data16</td><td>data26</td><td>data36</td><td>data46</td></tr>
    <tr style="color:#333333"><td>data17</td><td>data27</td><td>data37</td><td>data47</td></tr>
    <tr style="color:#333333"><td>data18</td><td>data28</td><td>data38</td><td>data48</td></tr>
    <tr style="color:#333333"><td>data19</td><td>data29</td><td>data39</td><td>data49</td></tr>

The output should be according to the imgae below, only cells starting with data1 and data3 should match as those are the ones from column 1 and 3.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.