Python实战之Selenium自动化测试web登录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python实战之Selenium自动化测试web登录相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
class LoginTest(object):

    def __init__(self):
        self.url = ‘‘
        self.uname = ‘‘
        self.pwd = ‘‘
        self.userEle = ‘‘
        self.pwdEle = ‘‘
        self.loginEle = ‘‘
        self.status = ‘‘
        self.statusCode = ‘‘

    def openUrl(self):
        self.driver = webdriver.Firefox()
        self.driver.get(self.url)
        time.sleep(10)

    def findLoginEle(self):
        # id = "user"
        self.userbTn = self.driver.find_element_by_id(self.userEle)
        self.pwdbTn = self.driver.find_element_by_id(self.pwdEle)
        self.loginbTn = self.driver.find_element_by_id(self.loginEle)

        # print(self.userId,self.pwdId,self.loginId)

    def logIn(self):
        self.userbTn.clear()
        self.userbTn.send_keys(self.uname)
        self.pwdbTn.clear()
        self.pwdbTn.send_keys(self.pwd)
        self.loginbTn.click()
        time.sleep(30)

    def checkStatus(self):
        try:
            self.statusbTn = self.driver.find_element_by_id(self.status)
            if self.statusbTn.text == self.statusCode:
                print(self.statusCode)
                return False
        except:
            print("Pass")
            return True

    def quitBrowse(self):
        self.driver.quit()

if __name__ == __main__:
    test1 = LoginTest()
    userInfo = {
       uname:[Administrator,USERID],
        pwd:[Aa1,PASSW0RD]
    }
    test1.url = https://10.245.53.64/designs/imm/index.php
    for uname,pwd in zip(userInfo[uname],userInfo[pwd]):
        print(uname,pwd)
        test1.uname = uname
        test1.pwd = pwd
        test1.userEle = user
        test1.pwdEle = password
        test1.loginEle = btnLogin_label
        test1.status = gblMessageDlgID_title
        test1.statusCode = Login failed.

    test1.openUrl()
    test1.findLoginEle()
    test1.logIn()
    if test1.checkStatus() == False:
        test1.quitBrowse()

 

以上是关于Python实战之Selenium自动化测试web登录的主要内容,如果未能解决你的问题,请参考以下文章

Selenium3 Python3 Web自动化测试从基础到项目实战之二浏览器的不同设置

Selenium3与Python3实战 Web自动化测试框架

Selenium3与Python3实战 Web自动化测试框架

Selenium3与Python3实战 Web自动化测试框架

Selenium3与Python3实战 Web自动化测试框架

Selenium3与Python3实战 Web自动化测试框架