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

注:識別驗證碼的圖片源要儘量清楚點。。要是太模糊或者線條顏色干擾太多,識別率感人


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章