title: How do I prevent a stale element reference and make a WebElement update itself in selenium?
body: <p>I'm a complete newbie at dealing with python and selenium, just started a week ago, so do excuse my mess of a code. I'm trying to extract all the 'structure_id' and 'd' information from elements with tag name  in <a href="http://atlas.brain-map.org/atlas?atlas=265297126#atlas=265297126&amp;plate=102339919&amp;structure=10155&amp;x=42476.80078125&amp;y=16377.599609375&amp;zoom=-7&amp;resolution=124.49&amp;z=2" rel="nofollow noreferrer">this website</a> and store each of them in a separate svg file. Here is a snippet of the code I'm having problems with:</p>
  <pre><code>for number in range(1,106):
      try:
          element = WebDriverWait(driver, 10).until(
              EC.presence_of_all_elements_located((By.ID, 'master_group'))
          )
          selected = driver.find_element_by_class_name('simstripImgSel')
  
          driver.get(driver.current_url)
          paths = driver.find_elements_by_tag_name('path')
          
          for path in paths:
              while True:
                  try:
                      structure = path.get_attribute('structure_id')
                      d = path.get_attribute('d')
                      break
                  except Exception as e:
                      print(e)
                      paths = driver.find_elements_by_tag_name('path')
                      continue
              if structure != None:
                  print('Attributes copied.')
                  for word, initial in data.items():
                      structure = structure.replace(word,initial)
                  filepath = Path('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg')
                  if filepath.is_file():
                      text = open('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg','r+')
                      rep = text.read()
                      rep = rep.replace('&lt;/svg&gt;','&lt;path id=&quot;')
                      text.close()
                      os.remove('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg')
                      time.sleep(0.2)
                      text = open('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg','w+')
                      text.write(rep+str(structure)+'&quot; d=&quot;'+str(d)+'&quot;&gt;&lt;/path&gt;&lt;/svg&gt;')
                      text.close()
                      print('File '+str(structure)+' modified in slice '+str(number)+'!')
                  else:
                      svg = open('C:\\Users\\justs\\Downloads\\Ordered_svgs\\slice'+str(number)+'\\'+str(structure)+'.svg','w+')
                      svg.write('&lt;svg id=&quot;the_svg_wrapper&quot; width=&quot;100%&quot; height=&quot;100%&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;path id=&quot;'+str(structure)+'&quot; d=&quot;'+str(d)+'&quot;&gt;&lt;/path&gt;&lt;/svg&gt;')
                      svg.close()
                      print('File '+str(structure)+' made in slice '+str(number)+'!')
  
          selected.send_keys('F')
          paths = 0
          print()
          
      except Exception as e:
          print('Error.')
          print(e)
          break
  
  print('Done!')
  driver.quit()
  
  </code></pre>
  <p>This works fine for the first page, but I need to extract paths for all 106 pages, and after pressing 'F' once (which moves on to the next page) I get a stale element reference at the line <code>structure = path.get_attribute('structure_id')</code>. Initially I thought the paths took some time to load, hence the while loop, but by the second page it gets stuck with never-ending stale element references.</p>
  <p>Explicit waits or refreshing the page didn't work too, I suspect the <code>driver.find_element_by_class_name</code> WebElement isn't updating at all (when I refreshed the page after moving on to the next page, the files I extracted ended up being the same as the first page, and I got a stale element reference by page 5 anyways). How do I solve this? Any help is appreciated!</p>