Python Selenium - selectbox - option is stale; either the element is no longer attached to the DOM

Python Selenium - selectbox - option is stale; either the element is no longer attached to the DOM

Hello,

When I want to click the whole selectbox with options in python selenium using this construction:


secondSelectId = 'frm-competibleList-competitionsStilter-form-coetitionsStage'
secondSelectBoxx = driver.find_element_by_id(secondSelectId)
secondCountOptions = len(secondSelectBoxx.find_elements_by_tag_name('option'))
secondOptions = [x for x in secondSelectBoxx.find_elements_by_tag_name("option")]

print('Total second TS: ', secondCountOptions)

# loop all options
for secondOption in secondOptions:
secondOptionTabValue = secondOption.get_attribute("value")
print(secondOptionTabValue)

select = Select(driver.find_element_by_id(secondSelectId))
# select by visible text
# select.select_by_visible_text('xyz')
# select by value
select.select_by_value(secondOptionTabValue)

time.sleep(1)



So an error will appear soon:

optionis stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed

Does anyone know what to do with it? I just want to click on the whole selectbox and this logically refreshes the page and that will be the problem. How to solve this in python selenium please? Thanks




REPLY


Good day,

error in Python Selenium "selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of < option > is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed" I solved as follows:

1.) load all option values into the array firstLoopArray

firstLoopArray = []
for firstValue in firstOptions:
firstLoopArray.append(firstValue.get_attribute("value"))
print (firstValue.get_attribute("value"))


2.) then the options firstLoopArray iterate in the cycle + add lambda driver

for firstOption in firstLoopArray:
print('Item firstOption: ', firstOption)
WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath("//option[@value='{0}']" .format(firstOption)))
print("Xpath: //option[@value='{0}']" .format(firstOption))



-------------------------------------------------------------------------------------------------------------
My other general footnotes
# select by text
select.select_by_visible_text('xyz')
# select by value
select.select_by_value(firstOption)

Komentáře

Vaše reakce na Python Selenium - selectbox - option is stale; either the element is no longer attached to the DOM

Související obsah

linux

python

selenium

web

Reference

Podívejte se na naše reference

Prohlédnout

Aplikace

Podívejte se na naše aplikace

Prohlédnout

Co umíme?

Podívejte se co umíme

Prohlédnout

Co umíme?

Vytváříme sofistikované aplikace pro náročné

Od webových aplikací přes android až po převodové můstky či složité informační systémy.

Podívejte se k nám

Máte ještě čas? Podívejte se na další rubriky

Tento web používá soubory cookie. Dalším procházením tohoto webu vyjadřujete souhlas s jejich používáním.. Více informací zde.