2016-03-15 30 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.

相關問題