【问题标题】:Deducing an html element via its adjacent values通过其相邻值推导出一个 html 元素
【发布时间】:2012-01-09 09:24:25
【问题描述】:

我有一个呈现类似于下表的内容的 html 页面:

nov.    service 1     [ click to download pdf ]
nov.    service 5     [ click to download pdf ]
nov.    service 3     [ click to download pdf ]
dec.    service 1     [ click to download pdf ]
dec.    service 9     [ click to download pdf ]

这是html:

<tr class="values">
    <td class="col-1 first">Nov 2011</td>
    <td class="col-2">Switzerland</td>
    <td class="col-4">7</td>
    <td class="col-5">24.56</td>
    <td class="col-6">CHF</td>
    <input width="65" height="16" type="image" name="0.0.9.7.3.1.11.1.11.17.1.8.13.1.1" src="/itc/images/btn-grey-download.png" /></td>
</tr>
<tr class="values">
    <td class="col-1 first">Nov 2011</td>
    <td class="col-2">United Kingdom</td>
    <td class="col-4">25</td>
    <td class="col-5">67.88</td>
    <td class="col-6">GBP</td>
    <input width="65" height="16" type="image" name="0.0.9.7.3.1.11.1.11.17.1.9.13.1.1" src="/itc/images/btn-grey-download.png" />
</tr>
<tr class="values">
    <td class="col-1 first">Oct 2011</td>
    <td class="col-2">Americas</td>
    <td class="col-4">14</td>
    <td class="col-5">2</td>
    <td class="col-6">USD</td>
    <input width="65" height="16" type="image" name="0.0.9.7.3.1.11.1.11.17.1.10.13.1.1" src="/itc/images/btn-grey-download.png" />
</tr>

我需要能够“选择”并单击 11 月份的所有按钮(目前我正在使用 Selenium)。我可以通过 CSS 选择器(通过 Firebug 确定)单击单个按钮,但是如何在 11 月份以编程方式为所有按钮执行操作?伪代码是:

if tr.values.col-1.startswith(<month>):
    click the button in that row

【问题讨论】:

  • 您能否添加更多有关当前适用于一个元素的 CSS 选择以及您要单击的按钮的 html 的信息?

标签: python html css xml selenium


【解决方案1】:

由于您使用的是 Selenium,您可能想尝试通过 XPath 进行选择。此 XPath 表达式将为您提供所有 &lt;input&gt; 元素在一行中,第一列中包含 2011 年 11 月:

//td[@class='col-1 first']/text()[normalize-space()='Nov 2011']/parent::*/parent::*/td/input

【讨论】:

  • 哇,这是一个强大的功能。谢谢
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多