#coding=utf-8
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.select import Select
class timing(object):
def __init__(self):
option = webdriver.ChromeOptions()
option.add_argument("--user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data")
self.driver = webdriver.Chrome(chrome_options=option)
self.driver.get('chrome://settings/clearBrowserData')
def open(self):
sleep(3)
# 定位清除缓存按钮
clearButton = self.driver.execute_script("return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('settings-section > settings-privacy-page').shadowRoot.querySelector('settings-clear-browsing-data-dialog').shadowRoot.querySelector('#clearBrowsingDataDialog').querySelector('#clearBrowsingDataConfirm')")
# 赋值时间范围筛选的js
js = 'return document.querySelector("body > settings-ui").shadowRoot.querySelector("#main").shadowRoot.querySelector("settings-basic-page").shadowRoot.querySelector("#basicPage > settings-section:nth-child(8) > settings-privacy-page").shadowRoot.querySelector("settings-clear-browsing-data-dialog").shadowRoot.querySelector("#clearFromBasic").shadowRoot.querySelector("#dropdownMenu")'
# 查询时间范围
Select(self.driver.execute_script(js)).select_by_value('4')
sleep(3)
# 清除缓存
clearButton.click()
sleep(6)
# 关闭
self.driver.close()
if __name__ == '__main__':
test = timing()
test.open()
|