1、配置文件设置,commonPara.conf
[login]
user = admin
password = iEMP1234
IP = 8.7.168.87
2、从配置文件读取IP、用户、密码登陆网管,并在几个页面间来回切换,模拟频繁打开某页面,持续一定时间,最后退出网管:
#coding=utf-8
import time
from selenium.webdriver.common.action_chains import ActionChains
import datetime
import configparser
from selenium import webdriver
config=configparser.ConfigParser()
config.read("conf\\commonPara.conf")
ip=config.get("login","IP")
user=config.get("login","user")
password=config.get("login","password")
def login(ip,user,password):
driver= webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(8)
driver.get("https://"+ip+":31943")
try:
driver.find_element_by_xpath("//*[@id='txf_username']").clear()
driver.find_element_by_id("txf_username").send_keys(user)
driver.find_element_by_xpath("//*[@id='txf_imtinfo']").clear()
driver.find_element_by_id("txf_imtinfo").send_keys(password)
driver.find_element_by_id("btn_submit").click()
time.sleep(2) #等待2秒 .//*[@id='1']/h3/a
driver.find_element_by_id("license_regist_15_1").click() #点击“登陆”
except Exception as e:
print("ValueError: {0}".format(e))
return driver
try:
driver=login(ip,user,password)
time.sleep(2)
#开始业务操作
article = driver.find_element_by_id("menu.com.iemp.system") #鼠标移动到“系统”菜单
ActionChains(driver).move_to_element(article).perform()
driver.find_element_by_xpath(".//*[@id='menu.com.huawei.iemp.notice.settings']").click() #进入转储设置页面
time.sleep(2)
start_time=datetime.datetime.now()
end_time=start_time+datetime.timedelta(days=8) #持续操作7天
print("start_time:"+start_time.strftime('%Y-%m-%d %H:%M:%S'))
print("end_time:"+end_time.strftime('%Y-%m-%d %H:%M:%S'))
while start_time<end_time:
try:
driver.find_element_by_xpath(".//*[@id='accordion.iemp.fm.alarmsetting.emailServer']/div/div/a").click()
time.sleep(2)
driver.find_element_by_xpath(".//*[@id='accordion.iemp.fm.alarmsetting.smgServer']/div/div/a").click()
time.sleep(2)
driver.find_element_by_xpath(".//*[@id='accordion.iemp.fm.alarmsetting.smgModem']/div/div/a").click()
time.sleep(2)
tart_time=datetime.datetime.now()
except Exception as e: #上述页签切换会一直进行,直到遇到异常,如:手动关闭了浏览器页签等
print("ValueError: {0}".format(e))
driver.find_element_by_id("login_logoutIcon").click()
driver.find_element_by_id("fw_btn_ok").click()
print("logout success:")
break
time.sleep(1)
print ("检测8天结束:")
except Exception as e:
print("ValueError: {0}".format(e))
finally:
driver.find_element_by_id("login_logoutIcon").click()
driver.find_element_by_id("fw_btn_ok").click()
print("logout success:")
driver.quit()