Hi I have this code which is generating me:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not
It's easily cause this problem if you use find_elements_by_xpath to get a set of elements first.
I suggest you find element again in for loop like this:
offer_type_list = context.driver.find_elements_by_xpath(Locators.all_elements_buttons_offer_type)
offer_type_text = []
for i in range(1, len(offer_type_list)+1):
# xpath example: (//div[@class='abc'])[1]
element_offer_type = context.driver.find_element_by_xpath("("+Locators.all_elements_buttons_offer_type+")["+str(i)+"]")
compare_announcement_text(context, option_name, element_offer_type)
offer_type_text.append(element_offer_type.text)
I faced same exception a time ago. Its caused because the element you are interacting is not loaded in the DOM at that moment. You can solve it with a webDriverWait waiting for the element to be displayed.
@Jonx -> It's not the only reason. Question is also about app, @ranger is using. If it's dynamic (with reloading elements e.g. cause new live data) it's also raises this issue. depending on time when data comes in (e.g. after getting reference to element but before performing action). This can be pain in the ass -,-
@Yun - like this idea, but also (if in testing), using Page Object Pattern, good idea is to use views, and then perform actions within try-except blocks, or actually defining Page elements as property methods, so You'll get clean code, and WebDriver will look for element each time call. That eases up issue.
Yeah, I know that this is not so "well-performance" solution, but - as always - trade offs for stable behaviour with live-dynamic apps.