def browser(): options = Options() chrome_binary_path = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" driver_path = r"driver\chromedriver.exe" options.binary_location = chrome_binary_path options.binary = chrome_binary_path b = webdriver.Chrome(service=Service(driver_path), options=options) b.implicitly_wait(10) yield b # quit the driver instance for cleanup b.quit() driver = browser()