正在使用 selenium 元素爬行表的特定元素

x33g5p2x  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(154)

我有下面的抽象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']")

有人帮忙吗?

ktecyv1j

ktecyv1j1#

要将 class 值为 *datatable__rowtd * 的第一个<tr>标识为 datatable__rowtd,可以用途:

row = driver.find_element(By.XPATH, "//table[@class='datatable datatable--test datatable--tester ']/tbody/tr[@class='datatable__rowtd']")

要标识 class 值为 datatable__rowtd 的前两个<tr>,可以按如下所示slicelist

rows = driver.find_elements(By.XPATH, "//table[@class='datatable datatable--test datatable--tester ']/tbody//tr[@class='datatable__rowtd']")[:2]

相关问题