selenium实例
from selenium import webdriver import time import datetime from datetime import timedelta def main(): #驱动路径 web_driver = r'C:\Program Files\Mozilla Firefox\geckodriver.exe' #调用浏览器 driver = webdriver.Firefox(executable_path = web_driver) #打开网址 driver.get('http://132.33.18.31/ucenter/login') #间隔5秒钟 #time.sleep(5) #最大化窗口 driver.maximize_window() #输入用户名密码 driver.find_element_by_id("username").send_keys("sgs_bijibin") driver.find_element_by_id("password").send_keys("1qaz2wsx!QAZ@WSX") #点击登录 driver.find_element_by_css_selector("[type='submit']").click() #确认跳转 driver.find_element_by_xpath("//*[@id=\"msg\"]/p[3]/a").click() #//*为符合条件的所有元素 # xpath = "//标签名[@属性='属性值']" ,多个条件可以用xpath= "//input[@type='XX' and @name='XX']" #打开查询页面 driver.get('http://132.33.18.31/web/operationanalysis/operationanalysisCompatible.html?tabname=performance') #选择粒度为小时 driver.find_element_by_xpath("//*[@class=\"itemConditions discretetime\"]/div[1]/div/div[2]").click() #移除日期字段的readonly属性 js = 'document.getElementById("fromDate").removeAttribute("readonly");' driver.execute_script(js) js1 = 'document.getElementById("toDate").removeAttribute("readonly");' driver.execute_script(js1) #选择小时 driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[2]/div[9]/div[3]/div[1]").click() #全选 driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[2]/div[9]/div[3]/div[2]/div[5]/div[1]").click() #确定 driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[2]/div[9]/div[3]/div[2]/div[5]/div[3]").click() #time.sleep(5) #输入关键字 driver.find_element_by_xpath("//*[@id=\"searchWords\"]").send_keys("洛阳") #点击筛选 driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[3]/div/div[1]/div[2]/div[2]").click() time.sleep(5) #点击选中 driver.find_element_by_xpath("//*[@id=\"treebycell_3_check\"]").click() #选择闲时关断指标 driver.find_element_by_xpath("//*[@id=\"jsKpiTemplates\"]/li[2]/a").click() #获取当前日期 s = datetime.datetime.now().date() #获取本月1日 l = datetime.datetime(s.year, s.month, 1).date() #print(s,l) #从月初循环取到今天(i为日期间隔) for i in range((s - l).days + 1): x = l + datetime.timedelta(days=i) #将日期格式转化为字符串格式 u = datetime.datetime.strftime(x, "%Y-%m-%d") #print(x,u) #u= "2020-05-04" #设置日期 driver.find_element_by_id("fromDate").clear() driver.find_element_by_id("fromDate").send_keys(u) driver.find_element_by_id("toDate").clear() driver.find_element_by_id("toDate").send_keys(u) #点击直接导出 driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[4]/div/div[2]").click() #输入导出文件名称 driver.find_element_by_xpath("//*[@id=\"taskName\"]").send_keys(u) #选择csv driver.find_element_by_xpath("//*[@id=\"csv\"]").click() #点击下载 driver.find_element_by_xpath("//*[@id=\"jsExportButton\"]").click() #获取alter对话框 dig_alert = driver.switch_to.alert dig_alert.accept() ·