我正在编写一个 Selenium 单元测试,它从触发页面刷新的下拉菜单中选择一个选项(不是完全刷新,但 JS 更改了 DOM)以根据该选择显示数据。我的测试用例可以在 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() 函数打印的评论。