#coding =utf-8from selenium import webdriver#from selenium.webdriver.remote import switch_to#from selenium.webdriver.common import alert#import unittest import time,osdef users_zidian (): #用户名用例用一个字典实现参数化调用 # users={' Zhengshuheng ': ' 123456 ', ' [email protected] ': ' 4 ', ' zhh ': ' 5 ', ' Zhengshuheng ': ', ' Zheng ': ', ' ': ', ' Z ': 5, ' [email protected] ': ' 15970066750 '} #用字典设计测试用例 # #输入正确的用户名, wrong password, enter the correct mailbox, wrong password, Enter the wrong user name and password, etc. use case # print (' succs read username and password! ') return usersfor user,pwd in users_zidian (). Items (): # Use the For loop and dictionary's items () method to traverse the dictionary # driver=webdriver. Firefox () driver.get (' Http://www.docin.com/app/login ') #豆丁网登录地址 # driver.find_element_by_id ("UserName1"). Clear () driver.find_element_by_id ("UserName1"). Send_keys (user) #传递用例user参数 # time.sleep (2) driver.find_element_by_id ("Password1"). Clear () driver.find_element_by_id (" Password1 "). Send_keys (PWD) #传递用例pwd参数 # time.sleep (5) driver.find_ Element_by_xpath ("//input[@value = ' login ']"). Click () #使用Xpthon定位登录按钮 # d=driver.switch_to_ Alert () .text #获取点击登录后, Pop-up dialog Contents # driver.switch_to_alert (). Accept () #获取对话框焦点, accepting dialog box Contents # if user== ': print (' Enter an empty user name, enter a blank password, Prompt as follows: ') elif pwd== ': print (' Enter the user name%s, enter the password is empty%s, prompt as follows: '% (user,pwd)) #打印输入的用户名和密码 # else: print (' Enter the user name%s, enter the password for%s, prompt as follows: '% (user,pwd)) #打印输入的用户名和密码 # &nbSp; print (d) #打印对话框内容 # driver.quit () "Execution results are as follows: >>> ================================ restart ====== ==========================>>> succs read username and password! Enter an empty user name, enter a blank password, Prompt as follows: Sorry, please enter your user name or email! Enter the user name [email protected], enter the password is 15970066750, prompt as follows: Sorry, please enter the verification code! Enter the user name Z, enter a password of 5, prompt as follows: Login mailbox or password Error! Enter the user name zhh, enter the password is 5, prompt as follows: Sorry, please enter the verification code! Enter the user name Zhengshuheng, enter the password is empty, prompt as follows: Please enter your login password! Enter the user name Zheng, enter the password is empty, prompt as follows: Please enter your login password! >>> "
This article is from the "Cheng Technology blog" blog, make sure to keep this source http://zhengshuheng.blog.51cto.com/1439780/1575461
A simple login automation test with Python selenium+webdriver-----------bean-NET login test