def _get_clipboard_data(self) -> str: if Copy._need_captcha_reg: if ( self._trader.app.top_window() .window(class_name="Static", title_re="验证码") .exists(timeout=1) ): file_path = "tmp.png" count = 5 found = False while count > 0: self._trader.app.top_window().window( control_id=0x965, class_name="Static" ).capture_as_image().save( file_path ) # 保存验证码 captcha_num = captcha_recognize(file_path) # 识别验证码 logger.info("captcha result-->" + captcha_num) if len(captcha_num) == 4: self._trader.app.top_window().window( control_id=0x964, class_name="Edit" ).set_text( captcha_num ) # 模拟输入验证码 self._trader.app.top_window().set_focus() pywinauto.keyboard.SendKeys("{ENTER}") # 模拟发送enter,点击确定 try: logger.info( self._trader.app.top_window() .window(control_id=0x966, class_name="Static") .window_text() ) except Exception as ex: # 窗体消失 logger.exception(ex) found = True break count -= 1 self._trader.wait(0.1) self._trader.app.top_window().window( control_id=0x965, class_name="Static" ).click() if not found: self._trader.app.top_window().Button2.click() # 点击取消 else: Copy._need_captcha_reg = False count = 5 while count > 0: try: return pywinauto.clipboard.GetData() # pylint: disable=broad-except except Exception as e: count -= 1 logger.exception("%s, retry ......", e)
def login(self, user, password, exe_path, comm_password=None, **kwargs): """ 登陆客户端 :param user: 账号 :param password: 明文密码 :param exe_path: 客户端路径类似 'C:\\中国银河证券双子星3.2\\Binarystar.exe', 默认 'C:\\中国银河证券双子星3.2\\Binarystar.exe' :param comm_password: 通讯密码, 华泰需要,可不设 :return: """ try: self._app = pywinauto.Application().connect(path=exe_path, timeout=2) except Exception: self._app = pywinauto.Application().start(exe_path) # 登陆界面对话框 dlg_login = self._app.window( title_re=self._config.LOGIN_DIALOG_TITLE, class_name="#32770") dlg_login.wait("ready", 100) # 帐号控件 edit_account = dlg_login["ComboBox1"].child_window( control_id=self._config.LOGIN_DIALOG_ACCOUNT_ID) edit_account.wait("ready", 10) # 密码控件 edit_password = dlg_login.child_window( control_id=self._config.LOGIN_DIALOG_PASSWORD_ID, class_name="Edit") edit_password.wait("ready", 5) # 附件验证方式:CAPTCHA-验证码, COMMUNICATE_PASSWORD-通讯密码 if self._config.LOGIN_DIALOG_ADDITIONAL_CONFIRM_TYPE == 'COMMUNICATE_PASSWORD': edit_commpsw = dlg_login['Edit3'] # 通讯密码 # edit_commpsw = dlg_login.child_window(control_id=self._config.LOGIN_DIALOG_COMMUNICATE_PASSWORD_INPUT_ID, class_name="Edit") # 确定 while dlg_login.exists(): try: edit_account.set_text(user) edit_password.set_text(password) edit_commpsw.set_text(comm_password) bt_confirm = dlg_login.child_window( control_id=self._config.LOGIN_DIALOG_CONFIRM_ID, class_name="Button") bt_confirm.wait("ready", 0.5) bt_confirm.click() except: break time.sleep(0.5) elif self._config.LOGIN_DIALOG_ADDITIONAL_CONFIRM_TYPE == 'CAPTCHA': edit_ocr = dlg_login.child_window( class_name="Edit", control_id=self._config.LOGIN_DIALOG_CAPTCHA_INPUT_ID ) # 验证码控件 # 输入验证码 file_path = tempfile.mktemp() + ".png" while dlg_login.exists(): try: # 输入帐号和密码 edit_account.set_text(user) edit_password.type_keys(password) ocr_img = dlg_login.child_window( control_id=self._config. LOGIN_DIALOG_CAPTCHA_IMAGE_ID) ocr_img.wait('ready', 10) im = ocr_img.capture_as_image() im.save(file_path) captcha_num = captcha_recognize(file_path) edit_ocr.set_text(captcha_num) # 输入验证码 # 点击确定 bt_confirm = dlg_login.child_window( control_id=self._config.LOGIN_DIALOG_CONFIRM_ID) bt_confirm.wait("ready", 1) bt_confirm.click() time.sleep(1) except: break time.sleep(0.5) while True: try: self._main = self._app.window( title_re=self._config.MAINWIN_TITLE) self._main.wait("ready", timeout=100) break except: time.sleep(0.5) # 关闭弹出窗口 self.close_pop_dialog()
def login(self, user, password, exe_path, comm_password=None, **kwargs): """ 登陆客户端 :param user: 账号 :param password: 明文密码 :param exe_path: 客户端路径类似 'C:\\Programs\\金长江网上交易汇智版\\xiadan.exe', 默认 'C:\\Programs\\金长江网上交易汇智版\\xiadan.exe' :param comm_password: 通讯密码, 华泰需要,可不设 :return: """ self._app = pywinauto.Application().start(exe_path) time.sleep(1) dialogs = self._app.windows(class_name="#32770", visible_only=True) dlg_wrapper = dialogs[0] dlg_children = dlg_wrapper.children(class_name='Edit', visible_only=True) edit_account = dlg_children[0] edit_account.set_edit_text(user) edit_password = dlg_children[1] edit_password.type_keys(password) edit_ocr = dlg_children[3] pic_ocr = dlg_wrapper.children(class_name='Static', visible_only=True)[3] file_path = tempfile.mktemp() + ".png" while True: try: if pic_ocr.is_visible(): im = pic_ocr.capture_as_image() im.save(file_path) captcha_num = captcha_recognize(file_path) print(captcha_num) edit_ocr.type_keys(captcha_num) # 输入验证码 # 点击确定 bt_confirm = dlg_wrapper.children(title='登录', class_name='Button')[0] bt_confirm.draw_outline() bt_confirm.click() time.sleep(1) except: break time.sleep(0.5) # cnt = 0 while True: try: pop = self._app.top_window() if pop.window_text() != self._config.MAINWIN_TITLE: pop.draw_outline() pop.close() self._main = self._app.window( title_re=self._config.MAINWIN_TITLE) self._main.wait("ready", timeout=2) break except: time.sleep(0.5) # 关闭弹出窗口 self.close_pop_dialog()