2016-03-15 48 views
1

我应该通过产品的所有选项单击一个Python函数:Python和硒“元素不附加到页面文件”

submit_button = driver.find_element_by_id('quantityactionbox') 

elementList = submit_button.find_elements_by_tag_name("option") 

for x in elementList: 
    x.click() 

之后,我点击了2个元素我得到这个错误:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document 

你能告诉我为什么这个错误设计器和我能做些什么来成功地通过所有元素?

回答

4

你有解释和The Element is not Attached to the DOM解决方案:

A common technique used for simulating a tabbed UI in a web app is to prepare DIVs for each tab, but only attach one at a time, storing the rest in variables. In this case, it's entirely possible that your code might have a reference to an element that is no longer attached to the DOM (that is, that has an ancestor which is "document.documentElement").

If WebDriver throws a stale element exception in this case, even though the element still exists, the reference is lost. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.

相关问题