获取 table 中第二行的 XPath

XPath to get the second row in the table

整个 table 的 XPath,它有 11 行:

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

结果:

结果是这样的:

HTML代码:

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

如何获取第二行的值 当我在控制台 space 下签入开发工具时,我的 xpath 有效。但是当我复制到我的代码 selenium 时我得到一个错误 xpath 找不到元素

我的 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]")

我的 C# 代码:

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

选项 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]")

选项 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]

如果您可以共享 URL(如果它是 public 站点)或 table 的 html 源,我们可以提供精确的 XPath。

要获取第二行的值,您可以使用以下任一方法 :

  • 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"));