# -*- coding: utf-8 -*- # !/usr/bin/env python """ 监测到 dist/static 文件夹变动后,自动清理chrome缓存 """ import os import time from base import init_env init_env(interactive=True) from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from watchdog.observers import Observer from watchdog.events import LoggingEventHandler driver = webdriver.Chrome(executable_path=os.environ['CHROME_PATH']) OBSERVING_PATH = '.' def get_clear_browsing_button(driver): """Find the "CLEAR BROWSING BUTTON" on the Chrome settings page.""" return driver.find_element_by_css_selector('* /deep/ #clearBrowsingDataConfirm') def clear_cache(driver, timeout=60): """Clear the cookies and cache for the ChromeDriver instance.""" # navigate to the settings page driver.get('chrome://settings/clearBrowserData') # wait for the button to appear wait = WebDriverWait(driver, timeout) wait.until(get_clear_browsing_button) # click the button to clear the cache get_clear_browsing_button(driver).click() # wait for the button to be gone before returning wait.until_not(get_clear_browsing_button) if __name__ == '__main__': print 'starting cache clearer' # TODO add handler to clear cache event_handler = LoggingEventHandler() observer = Observer() observer.schedule(event_handler, OBSERVING_PATH, recursive=True) #observer.add_handler_for_watch() observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() clear_cache(driver)