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

Dobrý den,

Když chci proklikat celý selectbox s options v python selenium pomocí této konstrukce:


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)




Tak se brzo objeví chyba:

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

Nevíte někdo co s tím? Chci prostě proklikat celý selectbox a tím logicky dochází k refresh stránky a v tom bude problém. Jak to prosím v python selenium vyřešit?


Díky



ODPOVĚĎ

 


Dobrý den,

chybu v 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" jsem vyřešil takto:

1.) načíst všechny option values do array firstLoopArray


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



2.) pak teprve options z firstLoopArray iterovat v cyklu + přidat 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))

 

Jiný příklad:

Do secondOptions si ukládáme všechny elementy ze selectboxu. Pokud však elementy secondOptions projedeme cyklem for, tak dostaneme chybu "....element is no longer attached to the DOM...".

Provedeme proto uložení id všech elementů do pole arrayOptions a provedeme tak další iterace již na arrayOptions a elementy znovu jednotlivě dohledáme dle id pomocí find_element, takže takto:

 

        secondOptions = [x for x in secondSelectBoxx.find_elements('tag name', "option")]

        # fix error:
        # 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
        # loop all items to get ids  of elements and put ids to arrayOptions
        arrayOptions = []
        for so in secondOptions:
            arrayOptions.append(so.get_attribute("id"))

        # then we can
        # loop all options
        # and we fix error:  element is no longer attached  
        for o in arrayOptions:
            print('secondOption: ', o)
            secondOption = driver.find_element('id', secondSelectId)

.....



-------------------------------------------------------------------------------------------------------------
Další moje obecné poznámky pod čarou

# jak selektovat dle textu
select.select_by_visible_text('xyz')
# select by value
select.select_by_value(firstOption)

Show english version

Související obsah

linux

python

selenium

web

Komentáře

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

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.