【问题标题】:Iterating Through a Table in Selenium Very Slow在 Selenium 中迭代一个表非常慢
【发布时间】: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%">&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 selenium selenium-webdriver


    【解决方案1】:

    您可以一次性找到相关的复选框,方法是使用 xpath 表达式按文本搜索问题节点,并在其中获取 td 以下兄弟和 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
    

    【讨论】:

    • 我无法让 xpath 表达式返回任何内容。我粘贴的 HTML 代码的 sn-p 是更大页面的一部分。这是返回整个表的 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
    • @user2565150 好的,但想法保持不变。你能分享你运行它的网址吗?谢谢
    • 该页面是企业系统的一部分,因此我无法发布网址。这是页面来源的粘贴:gist.github.com/anonymous/a83dcc324ea0b3370b3d
    • @user2565150 谢谢你,我已经改进了 xpath,请检查它是否适合你。
    • 破解了。在阅读大量其他 xpath 堆栈帖子后,在您的帮助和一些修改后,最终版本是 checkBox = 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/tr/td[(@ class="rowodd" 或 @class="roweven") 和 text()[contains(.,"%s")]]/following-sibling::td[2]/input[starts-with(@name, " selectionIndex")]' % k)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2023-04-11
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多