我有下面的抽象html代码,我想提取表数据的一部分:
<table class="datatable datatable--test datatable--tester ">
<tbody><tr class="datatable__rowth">
<tr class="datatable__rowtd datatable__rowtd--fakeheader">
<tr class="datatable__rowtd">
<tr class="datatable__rowtd">
<tr class="datatable__rowtd">
<tr class="datatable__rowtd">
<tr class="datatable__rowtd datatable__rowtd--fakeheader">
<tr class="datatable__rowtd">
<tr class="datatable__rowtd">
</tbody></table>
更具体地说,我想提取第一个<tr class="datatable__rowtd datatable__rowtd--fakeheader">
的<tr class="datatable__rowtd">
,但我不知道如何提取?
我已尝试执行以下命令:
rows = driver.find_elements(By.XPATH, "//table[@class='datatable datatable--test datatable--tester ']/tr[@class='datatable__rowtd']")
但这将返回预期的6 <tr class="datatable__rowtd">
,而我需要前4个。
此外,我尝试了下面的命令,它不工作:
rows = driver.find_elements(By.XPATH, "//table[@class='datatable datatable-test datatable--tester ']//tr[@class='datatable__rowtd datatable__rowtd--fakeheader'][0]//tr[@class='datatable__rowtd']")
有人帮忙吗?
1条答案
按热度按时间ktecyv1j1#
要将 class 值为 *
datatable__rowtd
* 的第一个<tr>
标识为datatable__rowtd
,可以用途:要标识 class 值为
datatable__rowtd
的前两个<tr>
,可以按如下所示slicelist: