Selenium: Issue 2383: StaleElementReferenceException : Element not found in the cache workaround

It is always frustrating¬† when you writing a good chunk of code and getting but from Selenium framework. Here is example of not working code that throws StaleElementReferenceException: Message: u’Element not found in the cache’¬†:


items = driver.find_elements_by_css_selector('a.fontSizeDefault')
for item in items:

    item.click() #Just for example
    # some more actions

Here is workaround for this problem:


spaceLinksCount = len(driver.find_elements_by_css_selector('a.fontSizeDefault'))

for i in range(spaceLinksCount):
    driver.find_elements_by_css_selector('a.fontSizeDefault')[i].click()
    # some more actions

But it is more slower to compare with first chunk (that is not working due to Selenium: Issue 2383). More details one can find in Selenium issue tracker http://code.google.com/p/selenium/issues/detail?id=2383