【发布时间】:2014-12-02 07:36:00
【问题描述】:
我有一个 selenium python 脚本,可以读取页面上的表格。该表有 3 列,第一列是 ID 列表,第三列是复选框。我遍历ID,直到找到我想要的ID,然后单击相应的复选框并保存。它工作正常,但非常慢,因为表可以是 4K 行。 这是当前代码(self.questionID 是包含我要查找的 ID 的字典):
k, v in self.questionID.items():
foundQuestion = False
i = 1
while foundQuestion is False:
questionIndex = driver.find_element_by_xpath('/html/body/div[1]/form/table[2]/tbody/tr/td[1]/table/tbody/tr/td/fieldset[2]/div/table[1]/tbody/tr/td/table/tbody/tr/td/div/table/tbody[%d]/tr/td[1]' % i).text
if questionIndex.strip() == k:
d = i - 1
driver.find_element_by_name('selectionIndex[%d]' % d).click()
foundQuestion = True
i +=1
这是表格的示例,只是前几行:
<thead>
<tr>
<th class="first" width="5%">ID</th>
<th width="90%">Question</th>
<th class="last" width="1%"> </th>
</tr>
</thead>
<tbody>
<tr>
<td class="rowodd">AG001 </td>
<td class="rowodd">Foo: </td>
<td class="rowodd"><input class="input" name="selectionIndex[0]" tabindex="30" type="checkbox"></td>
</tr>
</tbody>
<tbody>
<tr>
<td class="roweven">AG002 </td>
<td class="roweven">Bar </td>
<td class="roweven"><input class="input" name="selectionIndex[1]" tabindex="30" type="checkbox"></td>
</tr>
</tbody>
你可能猜到我不是蟒蛇忍者。是否有更快的方法来读取此表并找到正确的行?
【问题讨论】:
标签: python selenium selenium-webdriver