【发布时间】:2017-05-09 16:35:28
【问题描述】:
我目前正在阅读 Django/TDD 介绍书,但遇到了 StaleElementReferenceException 并卡住了。我一直在谷歌搜索并搜索 StackOverflow 以找到解决我的错误的方法,但我无法解决它。我的相关代码如下:
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 语句中。
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 错误,并且无法找到解决它的方法。有人有什么建议吗?
【问题讨论】: