Python 下拉选择刷新页面后,“元素不再附加到DOM”

Python 下拉选择刷新页面后,“元素不再附加到DOM”,python,unit-testing,selenium,Python,Unit Testing,Selenium,我正在编写一个Selenium unittest,它从下拉菜单中选择一个选项,触发页面刷新而不是完全刷新,但是JS更改DOM以基于该选择显示数据。可以看到我的测试用例 因此,重新加载数据后,selenium无法找到进一步的循环选项。不过我实际上不需要再循环了。 我可以做一个xpath查找,看看option.text是否在页面的H2元素中,但我的尝试失败了 for option in dropdown.find_elements_by_tag_name('option'): if self

我正在编写一个Selenium unittest,它从下拉菜单中选择一个选项,触发页面刷新而不是完全刷新,但是JS更改DOM以基于该选择显示数据。可以看到我的测试用例

因此,重新加载数据后,selenium无法找到进一步的循环选项。不过我实际上不需要再循环了。 我可以做一个xpath查找,看看option.text是否在页面的H2元素中,但我的尝试失败了

for option in dropdown.find_elements_by_tag_name('option'):
    if self.ff.find_element_by_xpath("//h2[contains(text(), option.text)"):
        pass    # Employee Selected
从下面的代码中,任何人都可以帮助避免这个“附加到DOM”错误吗?基本上,如果我可以选择选项[1]或其他什么,然后继续进行剩下的测试,那将是理想的

dropdown = self.ff.find_element_by_id('employeeDatabaseSelect')
for option in dropdown.find_elements_by_tag_name('option'):
    try:
        option.click()  # causes JS refresh which you need to wait for
    except Exception, e:
        print 'Exception ', e
else: sys.exit("Error: There are no employees for this employer")
print 'Dropdown: ', dropdown.getText()
WebDriverWait(self.ff, 50).until(lambda driver : driver.find_element_by_xpath("//h2[contains(text(), dropdown.getText())"))
我的stacktrace看起来像这样

 [exec] test_process (__main__.viewEmployeeUseCase) ...
 [exec] ERROR
 [exec]
 [exec] ===============================================================
 [exec] ERROR: test_process (__main__.viewEmployeeUseCase)
 [exec] ---------------------------------------------------------------
 [exec] Traceback (most recent call last):
 [exec]   File "viewEmployeeUnitTest.py", line 43, in test_process
 [exec]     print 'Dropdown: ', dropdown.getText()
 [exec] AttributeError: 'WebElement' object has no attribute 'getText'
 [exec]
 [exec] ---------------------------------------------------------------
 [exec] Ran 1 test in 16.063s
 [exec]
 [exec] FAILED (errors=1)
 [exec] Exception  Message: u'Element is no longer attached to the DOM'
 [exec] Exception  Message: u'Element is no longer attached to the DOM'
 [exec] Exception  Message: u'Element is no longer attached to the DOM'
 [exec] Exception  Message: u'Element is no longer attached to the DOM'
 [exec] Exception  Message: u'Element is no longer attached to the DOM'
 [exec] Exception  Message: u'Element is no longer attached to the DOM'
 [exec] Exception  Message: u'Element is no longer attached to the DOM'
 [exec] Dropdown:  Tearing Down!

那最后的拆掉!是从“我的拆卸”功能打印的注释。

正如您所看到的,刷新页面可能会导致元素引用的奇怪行为。您将看到的另一个常见错误是stale元素异常

从stacktrace中,我会尝试将第二行修改为最后一行:

print 'Dropdown: ', self.ff.find_element_by_id('employeeDatabaseSelect').getText()
这样您就有了对元素的新引用

类似地,另一个可能导致问题的区域是线路:

for option in dropdown.find_elements_by_tag_name('option'):
如果页面在迭代之间刷新,则下拉列表可能不再有效。如果是这样,我会尝试以下方法:

浏览所有选项并保留其值/文本列表 浏览值/文本非元素列表,然后找到与之匹配的选项元素
同样,每次都使用新的下拉列表引用。

您帮助我理解了这一点:每次从self.ff中查找下拉列表时,它仍然会产生相同的错误,因此我宁愿与1选项交互,因为我只需要确保选择列表。我不需要逐一检查清单中的每一项。在第一次单击之后,我如何才能跳出for循环?如果您只想检查第一个,那么您就不再需要该循环了。您可能只想尝试下拉菜单。通过标记名称选项查找元素。单击是。这应该是显而易见的,但我想我只是在看这个循环太久了!谢谢