title: Intermittent Stale Element Reference Exception
body: <p>I am writing a python selenium script to scrape data from a website. The script reads the employee identification number, name and date of birth from a CSV file and copies it to a Dictionary &amp; then enters data from each line into a form and then clicks a submit button.
  The next page that appears asks for the years of employment history which defaults to one year which is fine for my use case so the only thing the script does on this page is click the search button.
  The script works consistently for approximately 20 to 35 employees. But at some point the script will fail with the following stale element reference exception:</p>
  <blockquote>
  <p>Message: The element reference of <strong></strong> 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</p>
  </blockquote>
  <p>The relevant HTML for the Search button is:</p>
  <pre><code>&lt;div data-container=&quot;&quot; class=&quot;btn-label OSInline&quot;&gt;Search&lt;/div&gt;
  &lt;/button&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;&lt;div data-container=&quot;&quot; style=&quot;text-align: center;&quot; id=&quot;b8-ResetFilters2&quot;&gt;&lt;a data-link=&quot;&quot; href=&quot;#&quot; style=&quot;width: auto;&quot;&gt;&lt;span data-expression=&quot;&quot; style=&quot;width: auto;&quot;&gt;Reset Search&lt;/span&gt;&lt;/a&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;div
  </code></pre>
  <p>The relevant Python Selenium code is (GO DOWN TO &quot;*****&quot; TO SEE WHERE I THE SEARCH BUTTON IS):</p>
  <pre><code># Open MOCemployees.csv as readable file &amp; then convert to a Python Dictionary
  with open('MOCemployees.csv', 'r') as MOCemployees.:
      MOCemployees.Dict = csv.DictReader(MOCemployees.)
  
      for line in MCpatientsDict:
          MOCEmployeeNumber = (line['MCID'])
          LastName = (line['LastName'])
          FirstName = (line['FirstName'])
          DOB = (line['DOB'])
  
  # Explicit Wait for MOCEmployeeNumberInput then click on it and enter MOCEmployeeNumber
          MOCEmployeeNumberInput = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'b7-MOCEmployeeNumberInput')))
  # Locate LastNameInput and click on it and enter LastName
          LastNameInput = driver.find_element(By.ID, 'b7-LastNameInput')
          LastNameInput.click()
          LastNameInput.send_keys(LastName)
  # Locate FirstNameInput and click on it and enter FirstNameI
          FirstNameInput = driver.find_element(By.ID, 'b7-FirstNameInput')
          FirstNameInput.click()
          FirstNameInput.send_keys(FirstName)
  # Locate DOBInput and click on it and enter DOB
          DOBInput = driver.find_element(By.ID, 'b7-DOBInput')
          DOBInput.click()
  #DOBInput.send_keys('YYYY-MM-DD')
          DOBInput.send_keys(DOB)
  
  # Explicit Wait for Submit Button &amp; click on it
          SubmitButton = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.margin-top-m')))
          SubmitButton.click()
  
  # *****Explicit Wait for Search Button &amp; click on it to accept default 1 year of data *****THIS BUTTON IS NOT CLICKED INTERMITTENTLY AND THE STALE ELEMENT REFERENCE EXCEPTION OCCURS AT THIS POINT*****
          SearchButton = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-label')))
          SearchButton.click()
  #Wait
          time.sleep(3)
  
  # print MOCID, NAME &amp; then the value of the value attribute 
          RemainDeductible = (WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, &quot;b8-b36-Input_RemainAmtYr1&quot;))).get_attribute(&quot;value&quot;))
  
  #Save MOCID, Name &amp; Remaining Due to a CSV file
          POutput = ','.join((MOCEmployeeNumber, LastName, FirstName, DOB, RemainDeductible)) + '\n'
  
  #Locate Change Employee Link &amp; click on it
          SelectButton = driver.find_element(By.CSS_SELECTOR, '#b7-b3-Column4 &gt; div:nth-child(1) &gt; a:nth-child(1) &gt; span:nth-child(1)')
          SelectButton.click()
  </code></pre>
  <p>How can I prevent this stale element reference exception?</p>