0

XPath for the whole table which has 11 rows :

$x(".//div[@id='less_detail']/table/tbody/tr/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table/tbody/tr/td[1]")

Result:

The result was like this:

image

HTML Code:

<div id="less_detail"><table width="100%"><tbody><tr>
<td><table width="100%" table-layout="fixed">
<tbody><tr class="left-column"><td class="outer-table-cell" valign="top"><table class="table" width="100%" vspace="15">
<thead><tr class="yui-dt-even yui-dt-first"><td colspan="2"><h3> Details</h3></td></tr></thead>
<tbody>
<tr>
<td class="Label" width="250">Born (Age)</td>
<td class="Value">
    
</td>
</tr>
<tr>
<td class="Label" width="250">Gender</td>
<td class="Value">


    
</td>
</tr>
<tr>
<td class="Label" width="250">Country of Birth</td>
<td class="Value">-</td>
</tr>
<tr>
<td class="Label" width="250">Marital Status</td>
<td class="Value">-</td>
</tr>
<tr>
<td class="Label" width="250">Occupation</td>
<td class="Value">-</td>
</tr>
<tr>
<td class="Label" width="250">Ethnicity</td>
<td class="Value">-</td>
</tr>
<tr>
<td class="Label" width="250">Ethnicity</td>
<td class="Value">-</td>
</tr>
<tr>
<td class="Label" width="250">Ethnicity</td>
<td class="Value">-</td>
</tr>
<tr><td class="Label" width="250">Religion</td>
<td class="Value">-</td>
</tr><tr>
<td class="Label" width="250">Primary Language</td>
<td class="Value">-</td>
</tr>
<tr>
<td class="Label" width="250">Aliases</td>
<td class="Value">-</td>
</tr>
</tbody>
</table></td></tr>

</tbody>
</table></td></tr>
</tbody></table>
</div>

How do I get the value of second row My xpath works when I checked in the dev tools under console space . but when i copied to my code selenium I get an error the xpath can't find the element

My Xpath :

$x(".//div[@id='less_detail']/table/tbody/tr/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table/tbody/tr[2]/td[1]")

My c# code :

for(int i=1; i <= NumberofFields; i++)
        {
           
            lblpatientdetails.Add(driver.FindElement(By.XPath(".//div[@id='less_detail']/table/tbody/tr/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table/tbody/tr[" + i + "]/td[1]")).Text);
            if (driver.FindElements(By.XPath(".//div[@id='less_detail']/table/tbody/tr/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table/tbody/tr/td[@class='Value']")).Count > 0)
            {
                lblpatinetdetailsvalues.Add(driver.FindElement(By.XPath(".//div[@id='less_detail']/table/tbody/tr/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table/tbody[1]/tr[" + i + "]/td[@class='Value']")).Text);

            }
            else
            {
                lblpatinetdetailsvalues.Add(ClinicalPortalConstants.NOT_AVAILABLE);
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
PavanS
  • 187
  • 2
  • 11

2 Answers2

1

Option 1:

$x("(.//div[@id='less_detail']/table/tbody/tr/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table/tbody/tr[2]/td[1])[2]")

Option 2:

$x(".//div[@id='less_detail']/table/tbody/tr/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table/tbody/tr[2]/td[1]")[2]

We could provide the precise XPath if you can share either the URL (if it's a public site) or html source of the table.

supputuri
  • 12,238
  • 2
  • 14
  • 34
  • the above 2 options doesn't work ... i added my html to the question above please have a look .. thanks – PavanS Sep 10 '20 at 01:51
1

To get the value of second row you can use either of the following Locator Strategies:

  • XPath:

    IWebElement element = driver.FindElement(By.XPath("//div[@id='less_detail']/table//table//tbody//following::tr[2]/td[@class='Label']"));
    
  • CssSelector:

    IWebElement element = driver.FindElement(By.CssSelector("div#less_detail > table table tbody tr:nth-child(2) > td.Label"));
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217