前言:Windows 的 appium1.4 版本使用廣泛,但是一直無法抓取到 app的 total 彈出消息闸溃,造成了一定程度上斷言困難的情況。這里我嘗試了使用曲線救國的方式 ?使用Tesseract OCR (光學(xué)字符識別)和 pillow 來識別彈出框的內(nèi)容织阅。
1.使用截圖的方式識別
我使用的是python語言追他,原理是利用python-OCR識別圖片中的中文字符,python-OCR的安裝過程如下:
使用工具類:
1.pyocr
2.PIL
3.tesseract-ocr
1,2 可以直接使用pip安裝
3.安裝tesseract-ocr
http://jaist.dl.sourceforge.net/project/tesseract-ocr-alt/tesseract-ocr-setup-3.02.02.exe
下載后直接安裝缆毁,建議默認(rèn)安裝過程中的選項番川,安裝目錄默認(rèn)C:\Program Files (x86)\Tesseract-OCR
下載tesseract-ocr的中文庫,地址:https://codeload.github.com/tesseract-ocr/tessdata/zip/master脊框,里面包含tesseract多有的文字庫颁督,chi_sim.traineddata為簡體中文庫,將該文件放至C:\Program Files (x86)\Tesseract-OCR\tessdata目錄
參考代碼:
上面代碼中D:\test.png即為appium截取的圖片浇雹,lang=’chi_sim’代表是中文識別
打印輸出的就是整個test.png頁面上所包含的中文字符沉御。
2.使用抓取toast方式識別 ?(appium1.6 以上)
from selenium.webdriver.support.waitimport WebDriverWait
from selenium.webdriver.supportimport expected_conditions
from selenium.webdriver.common.byimport By
def find_toast(self,message, timeout=10, poll_frequency=0.5): #獲取toast ?message為需要抓到的toast文本
? ? xp_message= '//*[@text=\'{}\']'.format(message)
? ? element= WebDriverWait(self.driver, timeout, poll_frequency).until(
? ? ? ? expected_conditions.presence_of_element_located((By.XPATH, xp_message)))
? ? if element.text== message:return True
? ? else:return False