如何在Selenium中处理此StaleElementReferenceException? [英] How to deal with this StaleElementReferenceException in Selenium?

查看:78
本文介绍了如何在Selenium中处理此StaleElementReferenceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读Django / TDD书籍的简介,我遇到了StaleElementReferenceException并被卡住了。我一直在四处搜寻,并在StackOverflow上寻找错误的解决方案,但我一直无法解决。我的相关代码如下:

I'm currently going through an intro to Django/TDD book and I've hit a StaleElementReferenceException and got stuck. I've been googling around and searching StackOverflow for a solution to my error, but I haven't been able to work around it. My relevant code is as follows:

functional_tests.py

inputbox.send_keys(Keys.ENTER)
self.browser.implicitly_wait(3)
table = self.browser.find_element_by_id('id_list_table')
#rows = table.find_elements_by_tag_name('tr')
rows_ref = lambda: table.find_elements_by_tag_name('tr')
#self.browser.implicitly_wait(3)
foundBuy = False
for row in rows_ref():
    self.browser.implicitly_wait(3)
    rows_text = row.text
    if (rows_text == '1: Buy peacock feathers'):
        foundBuy = True
        break
if not (foundBuy):
    self.fail('Could not find "1: Buy peacock feathers" in rows\' text')
#self.assertIn('1: Buy peacock feathers', [row.text for row in rows_ref()])

错误在上面的代码中的 rows_text = row.text行出现。在我的原始代码中,它出现在底部的注释掉的self.assertIn语句中。

The error occurs in the above code at the "rows_text = row.text" line of code. In my original code, it occurred in the commented out self.assertIn statement at the bottom.

home.html

<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <form method="POST">
            <input name="item_text" id="id_new_item" placeholder="Enter a to-do item"/>
            {% csrf_token %}
        </form>
        <table id="id_list_table">
            {% for item in items %}
                <tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
            {% endfor %}
        </table>
    </body>
</html>

我输入的这本书的原始代码已被注释掉(减去了implicity_wait)。在上一本书中,代码没有问题,但是我一直在不断遇到StaleElement错误,无法找到一种方法来克服它。有人有任何建议吗?

The original code the book has me enter is commented out (minus the one implicity_wait). In my previous time going through this book, the code worked no problem, but I have been getting this StaleElement error non-stop and cannot figure out a way to get past it. Anyone have any suggestions?

推荐答案

首先,我想指出的是,隐式等待并不是很多人认为的。在您的代码中,您好像将它用作一种睡眠-但这远非如此。如果隐式等待找不到元素,则只需再次尝试等待这秒钟的时间。如果它第一次发现至少一个,则没有任何效果。一次设置就足够了,然后在整个会话期间对每个元素查找均有效。

First I would like to point out that the implicit wait is not what many people think is. In your code it looks like you are using it as a kind of sleep - but it is far from that. Implicit wait simply tries again for this amount of seconds if it can't find an element. If it finds at least one the first time, it has no effect. It is enough to set it once, and then it will be valid for each element lookup during the whole session. But it is not suitable to use it for waiting for special conditions.

这里似乎发生了以下情况(种族条件):

What seems to happen here is the following (kind of race condition):


  • 按Enter键

  • 在更新页面之前,驱动程序会找到所有当前可用的元素

  • 您尝试遍历元素,但同时页面会更新,使先前的查找无效。

您可以在按Enter键后使用实际的睡眠(不是推荐的解决方案,但可以进行实验),也可以等待以便实际动作发生/完成。 (例如,您可以使用自定义条件,该条件对当前行数进行计数,并等待其更改,或者使用JavaScript,直到所有发布请求完成。)

You can use an actual sleep after pressing Enter key (not a recommended solution, but for experimenting it works) or you can wait for the actual action to happen/finish. (For example you can use a custom condition that counts the current number of rows, and waits until it changes, or use a javascript that waits until all post requests finish.)

这篇关于如何在Selenium中处理此StaleElementReferenceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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