1. 程式人生 > >selenium登錄網銀密碼控件

selenium登錄網銀密碼控件

實現 main test nbsp location logon files elif 虛擬

嘗試登錄中行網銀,原來帶控件的密碼輸入框輸入的

最後用虛擬鍵盤實現的 , DD模擬鍵盤 http://www.ddxoft.com/

圖形驗證碼識別沒過,有時間再繼續

需要安裝 Tesseract-OCR IEdriver ,dd下載dll

# _*_ coding: utf-8 _*_
import os, sys
import uuid, time
import pytesseract
from selenium import webdriver
from ctypes import windll
from PIL import Image


def tesseract_file(file_path):
        
#my_env = {‘TESSDATA_PREFIX‘: r‘C:\Program Files\Tesseract-OCR\testdata‘} image = Image.open(os.path.realpath(file_path)) result = pytesseract.image_to_string(image) return result def login_abc(param): url = https://perbank.abchina.com/EbankSite/startup.do ie_driver
= C:\Program Files\Internet Explorer\IEDriverServer.exe #os.environ["webdriver.ie.driver"] = ie_driver browser = webdriver.Ie(ie_driver) browser.maximize_window() browser.get(url) time.sleep(1) browser.find_element_by_id(username).send_keys(param[
username]) #載入DD libpath = libs/DD81200x32.dll if sys.platform == win32 else libs/DD81200x64.32.dll dd_dll = windll.LoadLibrary(libpath) vk = {5: 205, c: 503, n: 506, z: 501, 3: 203, 1: 201, d: 403, 0: 210, l: 409, 8: 208, w: 302, u: 307, 4: 204, e: 303, [: 311, f: 404, y: 306, x: 502, g: 405, v: 504, r: 304, i: 308, a: 401, m: 507, h: 406, .: 509, ,: 508, ]: 312, /: 510, 6: 206, 2: 202, b: 505, k: 408, 7: 207, q: 301, "": 411, \\: 313, j: 407, `: 200, 9: 209, p: 310, o: 309, t: 305, -: 211, =: 212, s: 402, ;: 410} #需要組合shift的按鍵。 vk2 = {": "", #: 3, ): 0, ^: 6, ?: /, >: ., <: ,, +: =, *: 8, &: 7, {: [, _: -, |: \\, ~: `, :: ;, $: 4, }: ], %: 5, @: 2, !: 1, (: 9} def down_up(code): dd_dll.DD_key(vk[code], 1) dd_dll.DD_key(vk[code], 2) def dd(key): if i.isupper(): # 按下擡起 500是shift鍵碼 dd_dll.DD_key(500, 1) down_up(i.lower()) dd_dll.DD_key(500, 2) elif i in [email protected]#$%^&*()_+{}|:"<>?: dd_dll.DD_key(500, 1) down_up(vk2[i]) dd_dll.DD_key(500, 2) else: down_up(i) img = browser.find_element_by_id(vCode) x, y = img.location[x] + 50, img.location[y] + 50 #移動到密碼框點擊激活 dd_dll.DD_mov(x, y) dd_dll.DD_btn(1) time.sleep(1) #依次輸入密碼字符 for i in param[password]: dd(i) time.sleep(0.2) left, top = img.location[x], img.location[y] width, height = img.size[width] + left, img.size[height] + top os.makedirs(imgs) oldpath = imgs/ + str(uuid.uuid1()) + .png browser.save_screenshot(oldpath) oldpic = Image.open(oldpath) newpic = oldpic.crop((left, top, width, height)) picpath = imgs/ + str(uuid.uuid1()) + .png newpic.save(picpath) os.remove(oldpath) #tesseract可以識別簡單的圖形驗證碼 vcode = tesseract_file(picpath) browser.find_element_by_name(code).send_keys(vcode) #os.remove(picpath) browser.find_element_by_id(logo).click() time.sleep(1) logerr = browser.find_element_by_class_name(logon-error).text print(logerr) if __name__ == __main__: login_abc({username:13311111111, password:123456})

selenium登錄網銀密碼控件