title: How to properly close a Selenium WebDriver instance in python?
body: <p>I am writing a script that will check if the proxy is working. The program should:
  1. Load the proxy from the list (txt).
  2. Go to any page (for example wikipedia)
  3. If the page has loaded (even not completely) it saves the proxy data to another txt file.</p>
  
  <p>It must all be in the loop. It must also check whether the browser has displayed an error. I have a problem with always turning off the previous browser every time, after several loops several browsers are already open. </p>
  
  <p>Ps. I replaced the iteration with a random number</p>
  
  <pre><code>from selenium import webdriver
  import random
  from configparser import ConfigParser
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support import expected_conditions as EC
  import traceback
  
  while 1:
      ini = ConfigParser()
      ini.read('liczba_proxy.ini')
  
      random.random()
      liczba_losowa = random.randint(1, 999)
  
      f = open('user-agents.txt')
      lines = f.readlines()
      user_agent = lines[liczba_losowa]
      user_agent = str(user_agent)
  
      s = open('proxy_list.txt')
      proxy = s.readlines()
  
      i = ini.getint('liczba', 'liczba')
  
      prefs = {"profile.managed_default_content_settings.images": 2}
      chrome_options = webdriver.ChromeOptions()
      chrome_options.add_argument('--proxy-server=%s' % proxy[liczba_losowa])
      chrome_options.add_argument(f'user-agent={user_agent}')
      chrome_options.add_experimental_option("prefs", prefs)
      driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:\Python\Driver\chromedriver.exe')
      driver.get('https://en.wikipedia.org/wiki/Replication_error_phenotype')
  
      def error_catching():
          print("error")
          driver.stop_client()
          traceback.print_stack()
          traceback.print_exc()
          return False
  
  
      def presence_of_element(driver, timeout=5):
          try:
              w = WebDriverWait(driver, timeout)
              w.until(EC.presence_of_element_located((By.ID, 'siteNotice')))
              print('work')
              driver.stop_client()
              return True
          except:
              print('not working')
              driver.stop_client()
              error_catching()
  </code></pre>