Python – How to properly close a Selenium WebDriver instance in python?

How to properly close a Selenium WebDriver instance in python?… here is a solution to the problem.

How to properly close a Selenium WebDriver instance in python?

I’m writing a script to check if the agent is working. The plan should:
1. Load the agent from the list (txt).
2. Go to any page (e.g. Wikipedia)
3. If the page is loaded, even if it is not fully loaded, it saves the proxy data to another txt file.

It must all be in a loop. It must also check if the browser shows an error. I have a problem always closing the previous browser every time, and after a few loops, several browsers are already open.

Postscript. I replaced the iteration with a random number

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()

Solution

Do not comment on your code design:

To close a driver instance, use driver.close() or driver.quit() instead of your driver.stop_client().

The first one closes the browser window that sets the focus.
The second basically closes all browser windows and gracefully ends the WebDriver session.

Related Problems and Solutions