web-automation-pytest-demo/Lib/SeleniumClear.py

51 lines
1.4 KiB
Python

import tempfile
import platform
import shutil
import glob
import time
import os
def clear_selenium():
if platform.uname().system == 'Windows':
user_home = [os.environ.get('HOMEDRIVE'), os.environ.get('HOMEPATH')]
if user_home[0] and user_home[1]:
try:
shutil.rmtree('%s%s/.cache/selenium' % (user_home[0], user_home[1]))
except FileNotFoundError:
pass
def clear_driver_cache():
for cache in ['scoped_dir*', 'chrome_BITS*', 'chrome_url_fetcher*']:
for i in glob.glob('%s/%s' % (tempfile.gettempdir(), cache)):
try:
shutil.rmtree(i)
except (FileNotFoundError, PermissionError, WindowsError):
pass
def file_get_content(file, text=None):
if not os.path.exists(file):
return text
return open(file=file, mode='r', encoding='utf-8').read()
def file_put_content(file, text=None):
return open(file=file, mode='w', encoding='utf-8').write(text)
regular_clear = True
try:
selenium_clear_file = '%s/.selenium_clear_last' % tempfile.gettempdir()
selenium_clear_last = file_get_content(selenium_clear_file, '0')
try:
if int(selenium_clear_last) + 86400 < int(time.time()):
clear_selenium()
clear_driver_cache()
file_put_content(selenium_clear_file, str(int(time.time())))
except ValueError:
os.remove(selenium_clear_file)
except Exception:
pass