python+Selenium +pytesser3+PyMouse 自动化登录脚本

最近有时间学了学python  写了些自动化脚本来提升工作效率(zhuang bi)

不过作为一名功能测试,学习python还是有一些困难的,踩了一些坑,东拼西凑终于写好了一个能用来跑回归的脚本,还算蛮有成就感的~

我主要用了pytesser3来实现对图片验证码的识别功能;

使用Python selenium 的八种定位方式、配合Katalon Recorder(Selenium IDE for FF55+)#火狐插件  来抓取元素来实现自动化;

由于代码不规范,有些元素异常难抓 我偷懒用了 外部库PyMouse  直接点击屏幕坐标点,简单粗暴

下面是源码 一些相关资源我放到最后了

#coding = utf -8
from selenium import webdriver
import time ,unittest
import yzm #这个是我自己写的图片验证码函数  源码在下面
from pymouse import PyMouse # 外部库PyMouse
#shpt类继承unittest.TestCase类,从TestCase继承是告诉unittest,这是一个测试案例
class TEXT(unittest.TestCase):
	#setUp用户设置初始化,在测试用例执行前这个方法中的函数先被调用,这里将浏览器的调用URL的访问初始化
    def setUp(self):
        self.browser = webdriver.Firefox()
        self.base_url = "https://www.baidu.com"
		#脚本运行时,错误信息将被打印到这个空列表中
        self.verificationErrors = []
		#是否继续接受下一个警告
        self.accpt_next_alert = True

    def test_shpt_login(self):
        browser = self.browser
        browser.get("http://********/busiclient/")  #公司网址我就不发了。。。
        browser.maximize_window()  #将浏览器最大化显示           
#7输入正确用户名和密码
        browser.refresh() # 刷新方法 refresh
        browser.find_element_by_name("loginname").click()
        browser.find_element_by_name("loginname").send_keys("ad11")
        browser.find_element_by_name("password").click()
        browser.find_element_by_name("password").send_keys("111111")
        y = yzm.yzm_1(855,600,930,624)#这是坐标,用来截图的,交给pytesser3来识别
        browser.find_element_by_name("code").send_keys(y)
        browser.find_element_by_id("to-recover").click()
        time.sleep(5)
        while browser.current_url != "http://********"):#我们服务器有点慢,验证码错误提示又抓不到。。所以我是通过主页url来判断是否登录成功的。。
            #print("登录成功")
            print("重新输入验证码")
            browser.get("http://********/busiclient/")
            #browser.refresh() # 刷新方法 refresh
            browser.find_element_by_name("loginname").click()
            browser.find_element_by_name("loginname").send_keys("ad11")
            browser.find_element_by_name("password").click()
            browser.find_element_by_name("password").send_keys("111111")
            y = yzm.yzm_1(855,600,930,624)
            browser.find_element_by_name("code").send_keys(y)
            browser.find_element_by_id("to-recover").click()  
            time.sleep(5)
        print("商户登录成功")
        
#我的停车券     
        try:  
            browser.find_element_by_link_text(u"我的停车券").click()
            self.assertEqual(u"我的停车券", browser.title)
            print("我的停车券连接 pass")
        except AssertionError as e:  
            print ("我的停车券连接 gg" )
        browser.implicitly_wait(5)
#设置审批停车券
        name_1 = browser.find_element_by_xpath("//div[3]/div[2]/table/tbody/tr/td/div").text#名称
        type_1 = browser.find_element_by_xpath("//td[2]/div").text#类型
        much_1 = browser.find_element_by_xpath("//td[3]/div").text#额度
        way_1 = browser.find_element_by_xpath("//td[5]/div").text#使用方式
#        key_1 = browser.find_element_by_xpath("//td[6]/div").text#使用密码
        m = PyMouse()
        #print(m.position())#获取当前坐标的位置
        m.click(1334,285)#鼠标移动到xy位置
        #print(m.position())#获取当前坐标的位置
        browser.implicitly_wait(5)
        name_2 = browser.find_element_by_id("couponName").text#名称
        name_2 = browser.find_element_by_id("couponType").text#类型
        type_2 =browser.find_element_by_id("couponCount").text#额度
        much_2 =browser.find_element_by_xpath("//form[@id='couponForm']/div/div/div/i").click()#审批
        browser.find_element_by_link_text(u"确认").click()
        browser.implicitly_wait(5)
        name_3 = browser.find_element_by_xpath("//div[3]/div[2]/table/tbody/tr/td/div").text#名称
        type_3 = browser.find_element_by_xpath("//td[2]/div").text#类型
        much_3 = browser.find_element_by_xpath("//td[3]/div").text#额度
        way_3 = browser.find_element_by_xpath("//td[5]/div").text#使用方式
        try:  
            name_1 == name_2 == name_3
            type_1 == type_2 == type_3 
            much_1 == much_2 == much_3 
            way_3 =="审批"
            print("设置审批停车券 pass")
        except AssertionError as e:  
            print ("设置审批停车券 gg" )
        browser.implicitly_wait(5)
#设置密码停车券   
        name_1 = browser.find_element_by_xpath("//div[3]/div[2]/table/tbody/tr/td/div").text#名称
        type_1 = browser.find_element_by_xpath("//td[2]/div").text#类型
        much_1 = browser.find_element_by_xpath("//td[3]/div").text#额度
        way_1 = browser.find_element_by_xpath("//td[5]/div").text#使用方式
        m = PyMouse()
        m.click(1334,285)#鼠标移动到xy位置
        browser.implicitly_wait(5)
        name_2 = browser.find_element_by_id("couponName").text#名称
        name_2 = browser.find_element_by_id("couponType").text#类型
        type_2 =browser.find_element_by_id("couponCount").text#额度
        browser.find_element_by_xpath("//form[@id='couponForm']/div/div/div[2]/i").click()#密码
        browser.find_element_by_name("USAGE_PWD").click()
        browser.find_element_by_name("USAGE_PWD").send_keys("2333")
        browser.find_element_by_link_text(u"确认").click()
        browser.implicitly_wait(5)
        name_3 = browser.find_element_by_xpath("//div[3]/div[2]/table/tbody/tr/td/div").text#名称
        type_3 = browser.find_element_by_xpath("//td[2]/div").text#类型
        much_3 = browser.find_element_by_xpath("//td[3]/div").text#额度
        way_3 = browser.find_element_by_xpath("//td[5]/div").text#使用方式
        key_3 = browser.find_element_by_xpath("//td[6]/div").text#使用密码
        try:  
            name_1 == name_2 == name_3
            type_1 == type_2 == type_3 
            much_1 == much_2 == much_3
            way_3 =="审批"
            key_3 =="2333"
            
            print("设置密码停车券 pass")
        except AssertionError as e:  
            print ("设置密码停车券 gg" )
        browser.implicitly_wait(5)
        
#设置无限制停车券 
        name_1 = browser.find_element_by_xpath("//div[3]/div[2]/table/tbody/tr/td/div").text#名称
        type_1 = browser.find_element_by_xpath("//td[2]/div").text#类型
        much_1 = browser.find_element_by_xpath("//td[3]/div").text#额度
        way_1 = browser.find_element_by_xpath("//td[5]/div").text#使用方式
#        key_1 = browser.find_element_by_xpath("//td[6]/div").text#使用密码
        m = PyMouse()
        #print(m.position())#获取当前坐标的位置
        m.click(1334,285)#鼠标移动到xy位置
        #print(m.position())#获取当前坐标的位置
        browser.implicitly_wait(5)
        name_2 = browser.find_element_by_id("couponName").text#名称
        name_2 = browser.find_element_by_id("couponType").text#类型
        type_2 =browser.find_element_by_id("couponCount").text#额度
        much_2 =browser.find_element_by_xpath("//form[@id='couponForm']/div/div/div[3]/i").click()#无限制
        browser.find_element_by_link_text(u"确认").click()
        browser.implicitly_wait(5)
        name_3 = browser.find_element_by_xpath("//div[3]/div[2]/table/tbody/tr/td/div").text#名称
        type_3 = browser.find_element_by_xpath("//td[2]/div").text#类型
        much_3 = browser.find_element_by_xpath("//td[3]/div").text#额度
        way_3 = browser.find_element_by_xpath("//td[5]/div").text#使用方式
        try:  
            name_1 == name_2 == name_3
            type_1 == type_2 == type_3 
            much_1 == much_2 == much_3 
            way_3 =="无限制"
            print("设置无限制停车券 pass")
        except AssertionError as e:  
            print ("设置无限制停车券 gg" )
# =============================================================================
#         m.position()#获取当前坐标的位置
#         m.move(x,y)#鼠标移动到xy位置
#         m.click(x,y)#移动并且在xy位置点击
#         m.click(x,y,1|2)#移动并且在xy位置点击,左右键点击
# =============================================================================
            
        #抛异常
    def is_element_present(self, how, what):
        try:
            self.browser.find_element(by = how , value = what)
        except NoSuchElementException as e :
            return False
        return True
	
	#对弹窗异常的处理
    def is_alert_present(self):
        try:
            self.browser()
        except NoAlerPresentException as e:
            return False
        return True
	#关闭警告以及对得到问文本的处理
    def close_alert_and_get_its_text(self):
        try:
            alert = self.browser()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally:
            self.accpt_next_alert = True
			
	#tearDown方法在每个测试方法实行后调用,做所有用例执行完成的清理工作,如退出浏览器等
    def tearDown(self):
        self.browser.quit()
		#断言 对前面verificationErrors方法获得列表进行比对,如果列表不为空,就输出列表中的报错信息
        self.assertEqual([], self.verificationErrors)
			
if __name__ == "__main__":
	#unittset.mian方法来测试类中以test开头的用例
    unittest.main()


下面是图片验证码函数

#coding = utf -8
from PIL import Image
from PIL import ImageEnhance
from PIL import ImageGrab
import pytesser3

def yzm_1(x1,y1,x2,y2):
    im = ImageGrab.grab((x1,y1,x2,y2))
    im.save('文件路径\\Python36\\Lib\\site-packages\\\\pytesser3\\!.png')
    img=Image.open('文件路径\\Python36\\Lib\\site-packages\\pytesser3\\!.png')
    imgry = img.convert('L')#图像加强,二值化
    sharpness =ImageEnhance.Contrast(imgry)#对比度增强
    sharp_img = sharpness.enhance(2.0)
    sharp_img.save("文件路径\\Python36\\Lib\\site-packages\\pytesser3\\2.png")
    return pytesser3.image_file_to_string('2.png')

验证码识别.py 文件要放在pytesser3 文件夹下运行,不然会报错

https://pan.baidu.com/s/1bIzo1hVzjg4EYSnxLYMc2w 这里有一些资源  不过安装教程、报错处理什么的我不管了,请自行百度

里面的坐标识别工具不是病毒。。虽然这个图标看起来怪怪的;同时这个识别到的坐标和我的分辨率不一样但也能用,或许坐标和分辨率就是不一样??反正取到的坐标能用。。

提取码  1fmc

注:识别验证码的图片源要尽量清楚点。。要是太模糊或者线条颜色干扰太多,识别率感人


猜你喜欢

转载自blog.csdn.net/weixin_41635857/article/details/80291455