使用Python、Selenium和XPath创建动态行表

2024-09-30 01:28:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python中使用Selenium和XPath来获取表中的“SIRET”行。 我试过不同类型的xpath,但我做不到。 一个问题是“^{cl1}”$

以下是我访问站点时所执行的手动步骤:

站点只包含根域。 在我通过登录数据访问站点之后,我输入一个搜索条件,这将打开一个页面,在那里我必须单击一个链接,然后打开一个带有表格的弹出窗口。 表包含4行和8列,第一行包含列的名称,其他3行包含数据作为“SIRET”列。 根据从特定服务器接收的数据,这3行的位置会定期更改。 这就是为什么我要用“SIRET”文本来屏蔽这一行和他的值。在

我最后的数据应该是这样的:塞雷特646 90 0.2%$2.94 1.03 0.07 4.52。在

非常感谢您的意见。在

<div class="table_container">
<table>
    <tbody>
        <tr class="reportHead">.....</tr></tbody>
    <tbody>
        <tr class="reportRow  ">....</tr> 
        <tr class="reportRow  ">....</tr>
        <tr class="reportRow  ">
            <td data-actual="SIRET" class="reportKeyword">SIRET</td>
            <td class="td2">646</td>
            <td class="td1">90</td>
            <td class="rcr">0.2%</td>
            <td class="td1">$2.94</td>
            <td class="td1">1.03</td>
            <td class="td1">0.07</td>
            <td class="td1 rctl">4.52</td>
        </tr>
    </tbody>
    <tfoot style="display: none;">....</tfoot>
</table>


Tags: 数据类型站点seleniumtablexpathtrclass
3条回答

您可以像这样使用xpath

SIRET= driver.find_element_by_xpath("//td[@data-actual='SIRET']")

{cd1>然后可以使用

如果数据发生动态变化,则必须使用

^{pr2}$

如果我正确地理解了这个问题,那么您将尝试从动态变化的<td>节点获取字符串"SIRET"。为此,可以使用以下代码行:

print(driver.find_element_by_xpath("//td[@class='reportKeyword']").get_attribute("innerHTML"))

奇怪。事实上,解决方案并不复杂:

driver.find_element_by_xpath("//td[@data-actual='SIRET']/../td")

相关问题 更多 >

    热门问题