[python]代码库
from selenium import webdriver
from time import sleep
import time
import pickle
import string
import random
from selenium.webdriver.support.ui import Select
import re
import os
class m20_new:
def __init__(self,url):
self.driver=webdriver.Chrome()
self.driver.get(url)
def login(self,psw):
self.driver.find_element_by_id('logInput').send_keys(psw)
self.driver.find_element_by_id('bntLogin').click()
print('登录成功')
return driver
def login(url,psw):
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(1)
for i in range(1,2):
print('第%d次登录'%i)
print('登陆前,当前url:',driver.current_url)
driver.find_element_by_id('logInput').send_keys(psw)
sleep(1)
driver.find_element_by_id('bntLogin').click()
cookies=driver.get_cookies()
print(cookies)
driver.find_element_by_xpath('//*[@id="nav-list"]/li[5]/h3/a/label').click()
print('登录后,当前url:',driver.current_url)
sleep(1)
#退出登录
driver.find_element_by_xpath("//a[@class='login-out underline']").click()
print('退出后,当前url:',driver.current_url)
#sleep(1)
# sleep(1)
# driver.find_element_by_xpath('//*[@id="table"]/tbody/tr[1]/td[3]').send_keys('abc123456')
# sleep(5)
# driver.quit()
#重启
def m20_reset(url,psw):
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(1)
driver.find_element_by_id('logInput').send_keys(psw)
driver.find_element_by_id('bntLogin').click()
#系统维护
sleep(1)
driver.find_element_by_xpath('//*[@id="nav-list"]/li[8]/h3/a').click()
#重启
# driver.find_element_by_xpath('//*[@id="url-list"]/li[1]/a').click()
driver.find_element_by_xpath("//a[@href='#reboot']").click()
# driver.find_element_by_xpath('').click()
#重启确认框
#driver.implicitly_wait(2)
sleep(2)
driver.find_element_by_xpath('//div/button[@class="md-btn ok"]').click()
sleep(20)
def Create_MAC():
num=string.hexdigits
mac='02'
for i in range(5):
str=':'+''.join(random.sample(num,2))
mac+=str
print(mac)
return mac
def add_static_ip(url,psw):
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(5)
driver.find_element_by_id('logInput').send_keys(psw)
driver.find_element_by_id('bntLogin').click()
sleep(1)
#静态IP分配
driver.find_element_by_xpath('//*[@id="nav-list"]/li[3]/h3/a').click()
#新增-填写ip地址
for i in range(1,254):
try:
#新增
driver.find_element_by_id('addList').click()
sleep(1)
ip_addr='192.168.0.'+str(i)
driver.find_element_by_xpath("//div[@class='control-wrap clearfix']/input[@data-field='bindIPAddr']").send_keys(ip_addr)
#填写mac
mac='6C:4B:90:D3:7A:F8'
driver.find_element_by_xpath("//div[@class='control-wrap clearfix']/input[@data-field='bindMACAddr']").send_keys(Create_MAC())
#保存
driver.find_element_by_xpath("//button[@class='md-btn ok']").click()
sleep(1)
except BaseException as error:
print(error)
continue
driver.quit()
def cel_static_ip(url,psw):
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(5)
driver.find_element_by_id('logInput').send_keys(psw)
driver.find_element_by_id('bntLogin').click()
sleep(1)
#静态IP分配
driver.find_element_by_xpath('//*[@id="nav-list"]/li[3]/h3/a').click()
driver.find_element_by_xpath("//table[@id='onlineBindTable']/thead/tr/th[@class='check form-table-th']/label[@class='table-ckeck-l icon-check-off']").click()
def m20_login(url,psw):
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(5)
driver.find_element_by_id('logInput').send_keys(psw)
driver.find_element_by_id('bntLogin').click()
sleep(1)
return driver
def repeat_reset(psw,driver):
#静态IP分配
driver.implicitly_wait(5)
#联网设置页面
# driver.find_element_by_xpath('//*[@id="nav-list"]/li[2]/h3/a').click()
#系统维护
driver.find_element_by_xpath('//*[@id="nav-list"]/li[8]/h3/a').click()
#复位
driver.find_element_by_xpath('//a[@href="#reset"]').click()
#确认恢复出厂设置
driver.find_element_by_xpath('//button[@class="md-btn ok"]').click()
print('恢复出厂设置成功,延时120s')
sleep(120)
#输入密码
driver.find_element_by_xpath('//div[@class="password pwd"]/div/div/div/input').send_keys(psw)
driver.find_element_by_xpath('//div[@class="password cfm-pwd"]/div/div/div/input').send_keys(psw)
sleep(1)
driver.find_element_by_xpath('//button[@class="md-btn ok"]').click()
sleep(2)
print("激活页面,设置初始密码")
#下拉框选择,选择动态拨号
sleep(2)
driver.find_element_by_xpath('//*[@id="wan0"]/div/div[2]/div').click()
Select(driver.find_element_by_name("wanType")).select_by_value("dhcp")
driver.find_element_by_xpath('//*[@id="module-save"]').click()
driver.quit()
def Cur_time():
cur_time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
print('当前时间为%s'%(cur_time))
def wu_ap(driver):
Cur_time()
driver.find_element_by_xpath('//*[@id="nav-list"]/li[5]/h3/a').click()
#driver.find_element_by_xpath('//*[@id="virtualAC"]/li[1]/a').click()
sleep(1)
#关闭ap管理
driver.find_element_by_xpath('//*[@id="acManageEn"]/label').click()
print('关闭ap管理')
#10s后打开ap管理
sleep(10)
driver.find_element_by_xpath('//*[@id="acManageEn"]/label').click()
driver.find_element_by_xpath('//*[@id="module-save"]').click()
print('打开ap管理,等待3分钟')
sleep(60*4)
return(driver)
driver.quit()
#打印ap管理的
def check_ap(driver):
driver.find_element_by_xpath('//*[@id="nav-list"]/li[5]/h3/a').click()
driver.find_element_by_xpath('//*[@id="virtualAC"]/li[2]/a').click()
#检查ap在线状态
elements_stau=driver.find_elements_by_css_selector('.form-table-tr > td[data-field="currentststus"]')
#获取ap的ip地址,按顺序存放在列表里
ip_addr=[]
elements_ip=driver.find_elements_by_css_selector('td[data-field="ipAddr"]')
for j in elements_ip:
Content=j.get_attribute('textContent')
a=re.match('(.*?):',Content)
#print('对应ip地址:',a.group()[:-3])
ip_addr.append(a.group()[:-3])
#ap在线状态:
n=0
for i in elements_stau:
stat=i.get_attribute('textContent')
#print(i.get_attribute('textContent'))
if stat=='离线':
print('有ap未上线,停止运行,当前时间:')
os.system('smtpfromqq.py')
Cur_time()
return 0
elif stat=='在线':
print(ip_addr[n]+' 在线情况 '+stat)
n+=1
driver.quit()
if __name__=='__main__':
url='http://192.168.0.252/'
psw='qwe123456'
flag=1
i=1
while flag==1:
print('第%d次执行用例'%i)
dri=m20_login(url,psw)
dri=wu_ap(dri)
b=check_ap(dri)
if b==0:
flag=0
i+=1
'''
#复现ap离线问题:偶尔开关ap管理,ap不发discover。ap误认为还绑定的
for i in range(1,500):
dri=m20_login(url,psw)
print('登录成功')
wu_ap(dri)
#10分钟,等待ap上线
sleep(60*10)
'''
#login(url,psw)
#add_static_ip(url,psw)
'''
#反复恢复出厂设置
for i in range(1,2):
try:
dri=m20_login(url,psw)
print('登录成功')
sleep(2)
repeat_reset(psw,dri)
cur_time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
print('成功复位并登录第%d次,当前时间为%s'%(i,cur_time))
except Exception as a:
print('抛出异常:',a)
continue
'''
#login(url,psw)
#频繁重启
#设置重启次数
'''
a=1000
for i in range (1,a):
cur_time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
print("第%d次重启,当前时间:%s"%(i,cur_time))
try :
m20_reset(url,psw)
sleep(110)
except BaseException as a:
print('抛出异常:',a)
sleep(20)
continue
'''