Python+selenium实现登录脚本

import unittest
from selenium import webdriver
from time import sleep
class LoginCase(unittest.TestCase):
def setUp(self):
self.dr = webdriver.Firefox()
self.dr.maximize_window()
self.dr.get('http://www.imooc.com/') #cnblog登录页面
sleep(3)
self.dr.find_element_by_id("js-signin-btn").click()
sleep(3)

#定义登录方法
def login(self, username, password):
self.dr.find_element_by_name("email").send_keys(username)
sleep(2)
self.dr.find_element_by_name("password").send_keys(password)
self.dr.find_element_by_css_selector("[class='btn-red btn-full xa-login']").click()
def test_login_success(self):
self.login('zhangna@imooc.com', 'gjhkjjhghj') # 正确用户名和密码
sleep(3)
self.dr.get_screenshot_as_file("D:\\login_success.jpg") # 截图 可自定义截图后的保存位置和图片命名

def test_login_pwd_error(self):
self.login('zhangna@imooc.com', '123456')
sleep(3)
error_message = self.dr.find_element_by_id('signin-globle-error').text
self.assertIn('密码错误', error_message)
self.dr.get_screenshot_as_file("D:\\login_success.jpg")

def tearDown(self):
self.dr.quit()
sleep(2)
print('自动测试完毕!')
if __name__=='__main__':
unittest.main()


最后截图保存到本地
posted @ 2017-07-27 15:17  张利宁的杂货铺  阅读(2707)  评论(0编辑  收藏  举报