Selenium webdriver selenium-使用count函数编写xpath
对于这样的HTML页面,我将编写两条xpath语句来从td元素获取文本。第一个xpath是获取与我的需求匹配的th元素的位置Selenium webdriver selenium-使用count函数编写xpath,selenium-webdriver,xpath,Selenium Webdriver,Xpath,对于这样的HTML页面,我将编写两条xpath语句来从td元素获取文本。第一个xpath是获取与我的需求匹配的th元素的位置 count(.//th[contains(., 'Header5')]/preceding-sibling::*)+1 然后我将从上面返回的计数值放到这个xpath中,并获取文本 .//tr[2]/td[**count**] 如何将这两个xpath合并到单个xpath中并获得结果。我尝试过类似的方法,但它总是选择第一个td .//tr[2]/td[count(.//t
count(.//th[contains(., 'Header5')]/preceding-sibling::*)+1
然后我将从上面返回的计数值放到这个xpath中,并获取文本
.//tr[2]/td[**count**]
如何将这两个xpath合并到单个xpath中并获得结果。我尝试过类似的方法,但它总是选择第一个td
.//tr[2]/td[count(.//th[contains(., 'Header5')]/preceding-sibling::*)+1]
(.//tr[2]/td)[count(.//th[contains(., 'Header5')]/preceding-sibling::*)+1]
下面是html结构
<thead>
<th> Header1 </th>
<th> Header2 </th>
<th> Header3 </th>
<th> Header4 </th>
<th> Header5 </th>
<th> Header6 </th>
</thead>
<tbody>
<tr>
<tr>
<td> Cell1 </td>
<td> Cell2 </td>
<td> Cell3 </td>
<td> Cell4 </td>
<td> Cell5 </td>
<td> Cell6 </td>
</tr>
</tbody>
校长1
校长2
校长3
校长4
校长5
校长6
第1单元
细胞2
第三单元
第四单元
第五单元
第六单元
这个应该可以做到:
.//tr[2]/td[count(//th[contains(., 'Header5')]/preceding-sibling::th) + 1]
请注意,
/td[count(.//th)]
意味着th
节点的计数,这些节点是td
的后代,而/td[count(//th)]
意味着th
的计数,所以要小心上下文节点(点)是的,现在我明白了为什么我的代码不起作用了。谢谢你的澄清。