python+Selenium +pytesser3+PyMouse 自動化登入指令碼
阿新 • • 發佈:2019-02-11
最近有時間學了學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
注:識別驗證碼的圖片源要儘量清楚點。。要是太模糊或者線條顏色干擾太多,識別率感人