0

我有这个 html 源代码:

<table class="uiInfoTable profileInfoTable uiInfoTableFixed">
    <tbody>
        <tr>
            <th class="label">Birthday</th>
            <td class="data">February 4, 1988</td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <th class="label">Interested In</th>
            <td class="data">women</td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <th class="label">Gender</th>
            <td class="data">male</td>
        </tr>
    </tbody>
    //           etc....        
</table>

我想得到所有的值thtd它们th是:生日、感兴趣、关系状态和语言..

我知道它应该是这样的:

elements = driver.FindElements(By.XPath("//a[@class='uiInfoTable profileInfoTable
 uiInfoTableFixed']//span[@class='label' and (text()='Birthday' or text()='Intrested 
In' or text()='Relationship Status' or text()='Languages')]")).ToList();

任何帮助表示赞赏!

4

1 回答 1

1

我发现了三个主要问题:

  • 在字符串中换行以进行比较
  • 错误的元素名称(a而不是tablespan而不是任何东西)
  • 拼写错误“感兴趣”

无论如何,您最好选择所有<tr/>具有匹配表头单元格的元素,然后选择所有匹配的子元素。您也可以省略text()实际上可能造成伤害的调用。

这将起作用:

//table[@class='uiInfoTable profileInfoTable uiInfoTableFixed']//tr[
  th[
    @class='label' and
    (.='Birthday' or .='Interested In' or .='Relationship Status' or .='Languages')
  ]
]/*[local-name() = 'th' or local-name() = 'td']

这是一个 XPath 1.0 解决方案,它也适用于较新的 XPath 版本。使用较新的 XPath 版本,您可以更改为更短的

//table[@class='uiInfoTable profileInfoTable uiInfoTableFixed']//tr[
  th[@class='label' and
  . = ('Birthday', 'Interested In', 'Relationship Status', 'Languages')]
]/(td, th)
于 2013-07-13T16:44:59.637 回答