驗證碼字庫製作教程,傳送門:
https://forum.uibot.com.cn/thread-254.htm
https://forum.uibot.com.cn/thread-350.htm
https://forum.uibot.com.cn/thread-409.htm
2019-4-23更新,取驗證碼原圖終級解決方案:runjs下載瀏覽器原圖。
1、解決瀏覽器截圖,圖片縮放導致字體變形,字庫識別不准問題;
2、解決http cookie模式,瀏覽器獲取不到cookie,下載的驗證碼圖片驗證失敗問題;
3、注意跨域問題,驗證碼域名更網站域名必須一致。否則設置瀏覽器允許跨域。
源碼如下,重點是js部分:
import App
import WebBrowser
import UiElement
Import wmcode
dim iPID = ""
dim objJson = ""
dim dictRet = ""
dim sRet = ""
// 附件demo解壓到D盤根目錄
// 識別庫路徑
Dim datPath="d:\\demo\\遼寧1-11111.dat"
// 識別庫密碼
Dim password="11111"
// 識別庫路徑
Dim datPath1="d:\\demo\\遼寧-12345606.dat"
// 識別庫密碼
Dim password1="12345606
// 待識別驗證碼圖片路徑
dim imgPath
dim flag,vcode
dim bRet = ""
dim hWeb = ""
dim fun = '''
function(){
// 參數設置
var css = "#valimg",imgType = "image/png",postfix=".png";
// 獲取驗證碼圖片的元素
var img = document.querySelector(css);
// 創建畫布
var canvas = document.createElement("canvas");
// 驗證碼圖片,原始寬度
canvas.width = img.naturalWidth;
// 驗證碼圖片,原始高度
canvas.height = img.naturalHeight;
// 2D模式
var ctx = canvas.getContext("2d");
// 渲染圖片
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
// 轉爲base64,用於給a做文件下載
var dataURL = canvas.toDataURL(imgType);
// 生成一個a元素
var a = document.createElement('a');
// 創建一個單擊事件,模擬下載
var event = new MouseEvent('click');
// 設置圖片名稱
a.download = new Date().getTime() + postfix
// 將生成的URL設置爲a.href屬性
a.href = dataURL;
// 觸發a的單擊事件
a.dispatchEvent(event);
return a.download;
}
'''
// 打開網站
hWeb = WebBrowser.Create("chrome","http://train.lnsafety.com/mnzxlogin.asp",10000,{"bContinueOnError":false,"iDelayAfter":1000,"iDelayBefore":200,"sBrowserPath":"","bChromeUnsafe":false})
#icon("@res:c1j8qhon-seh4-9nf2-rc8f-u4a97bfnf082.png")
// 判斷驗證碼是否存在
bRet = UiElement.Exists({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":{"url":"http://train.lnsafety.com/mnzxlogin.asp","title":"模擬在線考試登錄","tagName":"IMG","attrMap":{"tag":"IMG","id":"valimg"},"index":0}},{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
// 如果驗證碼存在
If bRet = True
sRet = WebBrowser.RunJS(hWeb,fun,true,{"bContinueOnError":false,"iDelayAfter":1000,"iDelayBefore":200})
TracePrint(sRet)
imgPath = "C:\\Users\\Administrator\\Downloads\\" & sRet
iPID = App.Start(imgPath, "0", "1")
// 加載識別庫
flag = wmcode.LoadDat(datPath1,password1)
If flag
// 識別圖片
vcode = wmcode.getCodeFormFile(imgPath)
TracePrint(vcode)
#icon("@res:7rus4nui-cajr-0bu2-h6rk-itm4fi82kik3.png")
// 設置驗證碼
UiElement.SetValue({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":{"url":"http://train.lnsafety.com/mnzxlogin.asp","title":"模擬在線考試登錄","tagName":"INPUT","attrMap":{"tag":"INPUT","id":"vold"},"index":0}},vcode,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
TracePrint(vcode)
// 設置身份證
#icon("@res:9pcilhhc-sis8-c6sk-c48t-3pfcneq18i5i.png")
UiElement.SetValue({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":{"url":"http://train.lnsafety.com/mnzxlogin.asp","title":"模擬在線考試登錄","tagName":"INPUT","attrMap":{"tag":"INPUT","id":"username"},"index":0}},"430724198612182817",{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
// 設置選擇框的值
#icon("@res:aijmr1nd-guh0-ulnr-l8bo-kdnr6t21j8mp.png")
UiElement.SetSelect({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":{"url":"http://train.lnsafety.com/mnzxlogin.asp","title":"模擬在線考試登錄","tagName":"SELECT","attrMap":{"tag":"SELECT"},"index":0}},["主要負責人"],"text",{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
// 以js方法點擊按鈕
sRet = WebBrowser.RunJS(hWeb,"function(){\ndocument.querySelector(\"#aabb\").click();\nreturn 123\n}",true,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200})
// 以鼠標點擊按鈕
#icon("@res:k2bbgaji-aoso-ecpg-p3s9-45ao6gl23uko.png")
//Mouse.Action({"wnd":[{"cls":"Chrome_WidgetWin_1","title":"*","app":"chrome"},{"cls":"Chrome_RenderWidgetHostHWND","title":"Chrome Legacy Window"}],"html":{"url":"http://train.lnsafety.com/mnzxlogin.asp","title":"模擬在線考試登錄","tagName":"IMG","attrMap":{"tag":"IMG","id":"aabb"},"index":0}},"left","click",10000,{"bContinueOnError":false,"iDelayAfter":300,"iDelayBefore":200,"bSetForeground":true,"sCursorPosition":"Center","iCursorOffsetX":0,"iCursorOffsetY":0,"sKeyModifiers":[],"sSimulate":"simulate"})
else
TracePrint("識別庫加載失敗")
end if
End If