我目前正在瀏覽Django/TDD的書籍,並且遇到了StaleElementReferenceException並被卡住了。我一直在搜索並搜索StackOverflow來解決我的錯誤,但我一直無法解決它。我的相關代碼如下:如何在Selenium中處理這個StaleElementReferenceException?
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錯誤,並且無法找到一種方法來克服它。任何人有任何建議?