python - 在 Selenium 中迭代表非常慢

标签 python selenium selenium-webdriver

我有一个 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%">&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 忍者。有没有更快的方法来读取此表并找到正确的行?

最佳答案

通过使用 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/

相关文章:

python - 用于 isinstance() 检查的 dict_keys 的显式 python3 类型是什么?

python - 从 Google map 中抓取城市的经度和纬度

authentication - 使用 Selenium 处理浏览器身份验证

java - 使用 cssSelector 或 xPath Selenium Web 驱动程序在过滤器上应用按钮?

python 3 : doctest helper/internal functions?

python - 使用具有权重的路径列表检测网络上有问题的节点

python-3.x - 在通过 ChromeDriver 和 Selenium 通过 Python 3 执行测试时如何解决 Chrome 显示 "Aw, Snap!"页面

python - 在python中使用selenium获取特定div的HTML代码

selenium-webdriver - Selenium WebDriver 无法在 iframe 中定位元素,并抛出 NoSuchElementException

python - 无法使分屏滚动到底部