python+selenium 模拟登陆网管并实现页面切换

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()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值