我有一个 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 忍者。有没有更快的方法来读取此表并找到正确的行?
最佳答案
通过使用 xpath 表达式按文本搜索问题节点并获取其 td
,您可以一次性找到相关复选框。以下 sibling 和 input
里面:
checkbox = driver.find_element_by_xpath('//tr/td[1][(@class="rowodd" or @class="roweven") and text() = "%s${nbsp}"]/following-sibling::td[2]/input[starts-with(@name, "selectionIndex")]' % k)
checkbox.click()
请注意,它会抛出 NoSuchElementException
如果未找到问题和相关复选框。您可能需要捕获异常:
try:
checkbox = driver.find_element_by_xpath('//tr/td[1][(@class="rowodd" or @class="roweven") and text() = "%s${nbsp}"]/following-sibling::td[2]/input[starts-with(@name, "selectionIndex")]' % k)
checkbox.click()
except NoSuchElementException:
# question not found - need to handle it, or just move on?
pass
关于python - 在 Selenium 中迭代表非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27234879/