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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章