环境准备:
已安装好python,selenium;
已下载好对应浏览器驱动
以下是使用谷歌浏览进行操作:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest,time
def SuperLogin(username, password):
driver = webdriver.Chrome()
base_url = "http:192.168.1.1"
driver.get(base_url)
#superName = "super"
#superPass = "admin"
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys(username)
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys(password)
driver.find_element_by_id("LoginId").click()
time.sleep(3)
#在这WEB登录可能会遇到非法用户名登录会提示错误告警信息;在此要接受这个告警并退出
if username == "super" and password == "admin":
#由于登录界面和后台管理界面不在同一个iframe下,则需要转换iframe
fh = driver.find_element_by_xpath("//*[@id='mainFrame']")
driver.switch_to.frame(fh)
time.sleep(2)
#判断确定登录的是超级管理员界面,区别在于普通用户界面没有application菜单
appL = driver.find_element_by_xpath('//*[@id="mmApp"]').text
print(appL)
if appL :
print("****************后台是管理员登录界面,存在%s !!!****************" %appL)
else:
print("****************后台是普通用户登录界面!!!****************")
#定位元素到退出按钮
lgout = driver.find_element_by_xpath('//*[@id="exit_button