title: Why does NoSuchElementException and NoSuchWindowException keep popping up, causing my program to stop running?
body: <p>I am running a python program using Selenium. However, while my program is running, it will stop randomly and a NoSuchWindowException error or NoSuchElementException will pop up. This results in the program stopping. Does anyone know why this would happen?</p>
  <p><strong>This is the code for the program:</strong></p>
  <p><em>[Background] It is supposed to search for &quot;slots&quot; and it does so by going into the slots page, and if it's empty, it will return back to the original page. However, the back button will disappear once the slot is available, and so my bot will immediately take the slot, securing the booking.</em></p>
  <pre><code>no_slot = True
  while no_slot:
      for num in range(60):
          search_button = browser.find_element(By.NAME, &quot;btnSearch&quot;)
          WebDriverWait(EC.element_to_be_clickable(search_button), 10)
          search_button.click()
          try:
              noslot_back_button = browser.find_element(By.CSS_SELECTOR, 'input[name=&quot;btnBack&quot;]')
          except NoSuchElementException:
              # Proceed with booking (make sure that it is at the booking page (implementation)
              slot_radio_info = browser.find_element(By.NAME, 'slot')
              slot_id = slot_radio_info.get_attribute('id')
              radio_button = browser.find_element(By.CSS_SELECTOR, 'input[id=&quot;' + str(slot_id) + '&quot;]')
              radio_button.click()
              slot_submit_button = browser.find_element(By.CSS_SELECTOR, 'input[value=&quot;Submit&quot;]')
              WebDriverWait(EC.element_to_be_clickable(slot_submit_button), 4)
              browser.execute_script(&quot;arguments[0].click();&quot;, slot_submit_button)
              double_cfm_button = browser.find_element(By.CSS_SELECTOR, 'input[value=&quot;Confirm&quot;]')
              double_cfm_button.click()
              no_slot = False
          else:
              WebDriverWait(EC.element_to_be_clickable(noslot_back_button), 10)
              noslot_back_button.click()
  </code></pre>
  <p><strong>This is the NoSuchElementException error:</strong></p>
  <p>Traceback (most recent call last):   File &quot;C:\Users\Jonathan\PycharmProjects\BBDCBOT\BBDC_trial.py&quot;, line 85, in 
  search_button = browser.find_element(By.NAME, &quot;btnSearch&quot;)   File &quot;C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 1238, in find_element</p>
  <pre><code>return self.execute(Command.FIND_ELEMENT, {   File &quot;C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 418, in execute
  self.error_handler.check_response(response)   File &quot;C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\errorhandler.py&quot;, line 243, in check_resp onse
  raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {&quot;method&quot;:&quot;css selector&quot;, &quot;selector&quot;:&quot;[name=&quot;btnSearch&quot;]&quot;}   (Session info: chrome=97.0.4692.71) Stacktrace: Backtrace:
      Ordinal0 [0x00D1FDC3+2555331]
      Ordinal0 [0x00CB77F1+2127857]
      Ordinal0 [0x00BB2E08+1060360]
      Ordinal0 [0x00BDE49E+1238174]
      Ordinal0 [0x00BDE69B+1238683]
      Ordinal0 [0x00C09252+1413714]
      Ordinal0 [0x00BF7B54+1342292]
      Ordinal0 [0x00C075FA+1406458]
      Ordinal0 [0x00BF7976+1341814]
      Ordinal0 [0x00BD36B6+1193654]
      Ordinal0 [0x00BD4546+1197382]
      GetHandleVerifier [0x00EB9622+1619522]
      GetHandleVerifier [0x00F6882C+2336844]
      GetHandleVerifier [0x00DB23E1+541697]
      GetHandleVerifier [0x00DB1443+537699]
      Ordinal0 [0x00CBD18E+2150798]
      Ordinal0 [0x00CC1518+2168088]
      Ordinal0 [0x00CC1660+2168416]
      Ordinal0 [0x00CCB330+2208560]
      BaseThreadInitThunk [0x753EFA29+25]
      RtlGetAppContainerNamedObjectPath [0x774E7A9E+286]
      RtlGetAppContainerNamedObjectPath [0x774E7A6E+238]
  </code></pre>
  <p><strong>This is the NoSuchWindowException  error:</strong></p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\Jonathan\PycharmProjects\BBDCBOT\BBDC_trial1.py&quot;, line 86, in &lt;module&gt;
      search_button = browser.find_element(By.NAME, &quot;btnSearch&quot;)
    File &quot;C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 1238, in find_element
  
      return self.execute(Command.FIND_ELEMENT, {
    File &quot;C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 418, in execute
      self.error_handler.check_response(response)
    File &quot;C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\errorhandler.py&quot;, line 243, in check_resp
  onse
      raise exception_class(message, screen, stacktrace)
  selenium.common.exceptions.NoSuchWindowException: Message: no such window
    (Session info: chrome=97.0.4692.71)
  Stacktrace:
  Backtrace:
          Ordinal0 [0x00D1FDC3+2555331]
          Ordinal0 [0x00CB77F1+2127857]
          Ordinal0 [0x00BB2CD0+1060048]
          Ordinal0 [0x00BA5763+1005411]
          Ordinal0 [0x00BA45DD+1000925]
          Ordinal0 [0x00BA47C7+1001415]
          Ordinal0 [0x00BBABC0+1092544]
          Ordinal0 [0x00BB505B+1069147]
          Ordinal0 [0x00BB4BD5+1067989]
          Ordinal0 [0x00BB53AB+1069995]
          Ordinal0 [0x00BB5650+1070672]
          Ordinal0 [0x00BDE212+1237522]
          Ordinal0 [0x00BDE69B+1238683]
          Ordinal0 [0x00C09252+1413714]
          Ordinal0 [0x00BF7B54+1342292]
          Ordinal0 [0x00C075FA+1406458]
          Ordinal0 [0x00BF7976+1341814]
          Ordinal0 [0x00BD36B6+1193654]
          Ordinal0 [0x00BD4546+1197382]
          GetHandleVerifier [0x00EB9622+1619522]
          GetHandleVerifier [0x00F6882C+2336844]
          GetHandleVerifier [0x00DB23E1+541697]
          GetHandleVerifier [0x00DB1443+537699]
          Ordinal0 [0x00CBD18E+2150798]
          Ordinal0 [0x00CC1518+2168088]
          Ordinal0 [0x00CC1660+2168416]
          Ordinal0 [0x00CCB330+2208560]
          BaseThreadInitThunk [0x753EFA29+25]
          RtlGetAppContainerNamedObjectPath [0x774E7A9E+286]
          RtlGetAppContainerNamedObjectPath [0x774E7A6E+238]
  </code></pre>