【问题标题】:'Element is no longer attached to the DOM' after drop down selection refreshes the page下拉选择刷新页面后,“元素不再附加到 DOM”
【发布时间】:2012-07-17 14:29:51
【问题描述】:

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

因此,在重新加载数据后,硒无法找到进一步循环的选项。不过,我实际上不需要再循环了。
我可以做一个 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())"))

我的堆栈跟踪看起来像这样;

 [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!

最后的 Tearing Down! 是从我的 tearDown() 函数打印出来的评论。

【问题讨论】:

    标签: python unit-testing selenium


    【解决方案1】:

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

    从堆栈跟踪中,我会尝试将倒数第二行修改为:

    print 'Dropdown: ', self.ff.find_element_by_id('employeeDatabaseSelect').getText()
    

    这样你就有了对元素的全新引用。

    同样,可能导致问题的另一个区域是这条线:

    for option in dropdown.find_elements_by_tag_name('option'):
    

    如果页面在迭代之间刷新,dropdown 可能不再有效。如果是这种情况,我会试试这个:

    1. 浏览所有选项并列出它们的值/文本
    2. 浏览值/文本列表(不是元素),然后找到与其匹配的选项元素

    同样,这是为了让您每次都使用 dropdown 的新引用。

    【讨论】:

    • 你帮助我理解了这一点 :) 每次它仍然产生相同的错误时从 self.ff 中查找下拉列表,所以我宁愿只与 1 个选项进行交互,因为我只需要确保列表被选中。我不需要遍历列表中的每一个。第一次点击后如何跳出 for 循环?
    • 如果只想检查第一个,则不再需要循环。你可能只想试试dropdown.find_element_by_tag_name('option').click()
    • 啊,是的。这应该很明显,但我想我已经看那个循环太久了!谢谢!
    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 2014-06-06
    • 2014-10-05
    • 1970-01-01
    • 2017-11-19
    相关资源
    最近更新 更多