硒中的表迭代非常慢 [英] Iterating Through a Table in Selenium Very Slow

查看:99
本文介绍了硒中的表迭代非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个硒python脚本,可以读取页面上的表格.该表有3列,第一列是ID列表,第三列是一个复选框.我遍历这些ID,直到找到所需的ID,然后单击相应的复选框并保存.它可以正常工作,但是非常慢,因为该表可以是4K行. 这是当前代码(self.questionID是具有我要查找的ID的字典):

I have a selenium python script that reads a table on a page. The table has 3 columns, the first is a list of IDs and the 3rd is a check box. I iterate through the IDs until I find the one I want then then click the corresponding check box and save. It works fine but is very slow as the table can be 4K rows. This is the current code (self.questionID is a dictionary with the IDs I'm looking for):

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

这是表格的样本,只是前几行:

This is a sample of the table, just the first couple of rows:

<thead>
<tr>
    <th class="first" width="5%">ID</th>
    <th width="90%">Question</th>
    <th class="last" width="1%">&nbsp;</th>
</tr>
</thead>
<tbody>
    <tr>
        <td class="rowodd">AG001&nbsp;</td>
        <td class="rowodd">Foo:&nbsp;</td>
        <td class="rowodd"><input class="input" name="selectionIndex[0]" tabindex="30" type="checkbox"></td>
    </tr>
</tbody>
<tbody>
    <tr>
        <td class="roweven">AG002&nbsp;</td>
        <td class="roweven">Bar&nbsp;</td>
        <td class="roweven"><input class="input" name="selectionIndex[1]" tabindex="30" type="checkbox"></td>
    </tr>
</tbody>

您可能会猜到我不是python忍者.有没有一种更快的方法来读取此表并找到正确的行?

As you can probably guess I'm no python ninja. Is there is a quicker way to read this table and locate the correct row?

推荐答案

通过使用xpath表达式按文本搜索问题节点并获取它的td跟随同级并在其中input:

You can find the relevant checkbox in one go by using an xpath expression to search a question node by text and to get it's td following sibling and input inside it:

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 如果未找到问题及其相关复选框.您可能需要捕获异常:

Note that it would throw NoSuchElementException in case a question and a related to it checkbox is not found. You probably need to catch the exception:

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

这篇关于硒中的表迭代非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆