title: Python Selenium Error: invalid session id
body: <p>I tried to start web driver -&gt; randomly time sleep -&gt; close web dirver
  But it occured &quot;invalid session id&quot;</p>
  <p>Does anyone know how to fix this problem?, plz</p>
  <p>this is the following code</p>
  <pre><code>    from tkinter import *
      from tkinter import messagebox
      from selenium import webdriver
      from selenium.webdriver.common.action_chains import ActionChains
      from selenium.webdriver.common.keys import Keys
      import pyautogui
      import chromedriver_autoinstaller
      import datetime
      import time
      import pyperclip
      import csv, os
      import sys
      import random
      
      mobile_emulation = { &quot;deviceName&quot;: &quot;iPhone 6/7/8&quot; }
      
      
      chrome_ver = chromedriver_autoinstaller.get_chrome_version().split('.')[0]  
      chromedriver_path = f'./{chrome_ver}/chromedriver.exe'
      
      options = webdriver.ChromeOptions()
      options.add_argument(&quot;start-maximized&quot;)
      options.add_argument(&quot;disable-infobars&quot;)
      options.add_argument(&quot;--disable-extensions&quot;)
      options.add_experimental_option('excludeSwitches', ['enable-logging'])
      options.add_experimental_option(&quot;mobileEmulation&quot;, mobile_emulation)
      
      driver = webdriver.Chrome(chromedriver_path, chrome_options = options)
      
      url = 'https://google.com/'
      
      driver.get(url)
      
      rndNum = random.randint(350,700)
      
      now = time.localtime()
      nowTime = str(now.tm_hour)+'HOUR'+str(now.tm_min)+'MIN'+str(now.tm_sec)+'SEC'
      
      print('------------------&gt; RANDOM TIME : ',rndNum//60,'MIN ', rndNum%60, 'SEC')
      print(&quot;DRIVER FINISH START&quot;, nowTime)
      
      
      time.sleep(rndNum)
      
      driver.close()
      
      driver.implicitly_wait(5)
      
      
      time.sleep(1)
          
      driver.switch_to.window(driver.window_handles[0])
      driver.implicitly_wait(5)
      
      isSearch = 1
      now = time.localtime()
      nowTime = str(now.tm_hour)+'HOUR'+str(now.tm_min)+'MIN'+str(now.tm_sec)+'SEC'
      
      print(&quot;DRIVER FINISH END&quot;, nowTime)
  </code></pre>
  <p>and this is the error</p>
  <pre><code>Traceback (most recent call last):
  
    File &quot;C:/Users/Desktop/testDriver.py&quot;, line 47, in &lt;module&gt;
      driver.implicitly_wait(5)
    File &quot;C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 911, in implicitly_wait
      self.execute(Command.SET_TIMEOUTS, {
    File &quot;C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 321, in execute
      self.error_handler.check_response(response)
    File &quot;C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py&quot;, line 242, in check_response
      raise exception_class(message, screen, stacktrace)
  selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
  </code></pre>