title: Selenium Python: execute_script is executing only once in a loop
body: <p>I want to perform following task 3 times without closing browser.
  Open Google.com in a Tab.
  Open New Tab 
  Then close tab containing Google.com
  Open Google.com in previous newly opened tab.</p>
  
  <p>I am using following code to open new tab:</p>
  
  <pre><code>browser.execute_script("window.open('', 'new_tab')")
  </code></pre>
  
  <p>But when executed in a loop it only gets executed once.</p>
  
  <p>I have printed Number of Window Handles which suggests that execute_script is executed only once.</p>
  
  <p>My Full Code:</p>
  
  <pre><code>cpath="C:/Users/Gupta Niwas/Downloads/Softwares/Browsers/Drivers/chromedriver_win32/chromedriver.exe"
  chrome_options = webdriver.ChromeOptions()
  chrome_options.add_argument("--incognito")
  chrome_options.add_argument("start-maximized")
  #browser = webdriver.Firefox(executable_path=fpath)
  browser = webdriver.Chrome(chrome_options=chrome_options,executable_path=cpath)
  
  
  for i in range(3):
  
      browser.get("https://google.com")
      print(len(browser.window_handles))
  
      print("current:",browser.current_window_handle)
  
      browser.execute_script("window.open('', 'new_tab')")
  
      print(len(browser.window_handles))
      next_tab=browser.window_handles[len(browser.window_handles)-1]
      print(next_tab)
      print(browser.title)
      browser.close()
      print(len(browser.window_handles))
  
      browser.switch_to_window(next_tab)
      browser.delete_all_cookies()
  </code></pre>
  
  <p>On the 2nd loop it throws an Exception:</p>
  
  <pre><code>runfile('C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi/temp5.py', 
  
  wdir='C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi')
  1
  current: CDwindow-75E62D95A2A8A7808C5AC369A8070641
  2
  CDwindow-F1C8AFC0E5742A2E55CEA17FCD951D1D
  Google
  1
  1
  current: CDwindow-F1C8AFC0E5742A2E55CEA17FCD951D1D
  1
  CDwindow-F1C8AFC0E5742A2E55CEA17FCD951D1D
  Google
  Traceback (most recent call last):
  
    File "&lt;ipython-input-1-12046950abfa&gt;", line 1, in &lt;module&gt;
      runfile('C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi/temp5.py', wdir='C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi')
  
    File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
      execfile(filename, namespace)
  
    File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
      exec(compile(f.read(), filename, 'exec'), namespace)
  
    File "C:/Users/Gupta Niwas/Downloads/Programming/Projects/Mi/temp5.py", line 34, in &lt;module&gt;
      print(len(browser.window_handles))
  
    File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 719, in window_handles
      return self.execute(Command.GET_WINDOW_HANDLES)['value']
  
    File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
      self.error_handler.check_response(response)
  
    File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
      raise exception_class(message, screen, stacktrace)
  
  WebDriverException: no such session
    (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64)
  </code></pre>