Xpath 获取课堂上的所有元素,这些元素的值是单词的一部分
我有以下html源代码:Xpath 获取课堂上的所有元素,这些元素的值是单词的一部分,xpath,Xpath,我有以下html源代码: <table class="uiInfoTable profileInfoTable uiInfoTableFixed"> <tbody> <tr> <th class="label">Birthday</th> <td class="data">February 4, 1988</td> </
<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>
感谢您的帮助 我发现了三个主要问题:
- 要比较的字符串中的换行
- 元素名称错误(
而不是a
,table
而不是其他名称)span
- 拼写错误“intrest”
元素,然后选择所有匹配的子元素。此外,您还可以省略实际调用的text()
调用
这将有助于:
//表[@class='uiInfoTable profileInfoTable uiInfoTableFixed']//tr[
th[
@class='label'和
(.='birth'或.='interesting'或.='Relationship Status'或.='Languages')
]
]/*[local-name()='th'或local-name()='td']
这是一个XPath 1.0解决方案,也适用于较新的XPath版本。对于较新的XPath版本,您可以更改为更短的版本
//表[@class='uiInfoTable profileInfoTable uiInfoTableFixed']//tr[
th[@class='label'和
.=(‘生日’、‘感兴趣’、‘关系状态’、‘语言’)]
]/(运输署,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();