def do_keypress(win_title="",title="",url="",selector="",text="",waitfor=WAIT_FOR): __logger.debug('do_keypress:[' + str(title) + '][' + str(selector) + '][' + str(text) + ']') try: ''''如果指定窗口''' if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) text = encrypt.decrypt(text) param = get_param(title, url,selector,text) starttime = time.time() while True: dll.doKeypress(param) err = string_at(dll.getLastError(), -1).decode('utf-8') if err == "": return True else: runtime = time.time() - starttime if runtime >= waitfor: __logger.debug(r'Operation timeout ' + err) raise Exception(err) __logger.debug(r'Attempt Failure - Wait for Attempt to Acquire ' + err) time.sleep(TRY_INTERVAL) except Exception as e: raise e
def control_send_cs(win_title=None, text=None, waitfor=WAIT_FOR): ''' :param win_title: 窗口标题 :param text: 设置参数 :param waitfor: 等待时间 :return: ''' __logger.debug('str control send:[win:' + str(win_title) + ']' + str(text)) starttime = time.time() try: while True: ''''如果指定窗口''' if (win_title != None): ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) text = encrypt.decrypt(text) result = str_dll.setElementValue(text) err = string_at(str_dll.getLastError(), -1).decode('utf-8') if err == "": return result else: runtime = time.time() - starttime if runtime >= waitfor: __logger.debug(r'timeout ' + err) raise ExcuteError('control send error:[' + win_title + ']') __logger.debug(r'try fail ,wait next' + err) time.sleep(TRY_INTERVAL) except Exception as e: raise e
def get_element_rect(win_title=None,title=None,url=None,selector=None,waitfor=WAIT_FOR): __logger.debug('get_element_rect:[' + str(title) + '][' + str(selector) + ']') try: if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) starttime = time.time() while True: param = get_param(title, url, selector) result = dll.getElementRect(param) result_pos_json = string_at(result, -1).decode('utf-8') result_pos_dic = json.loads(result_pos_json) if int(result_pos_dic["retCode"]) == 1: print('----------',result_pos_dic["x"], result_pos_dic["y"], result_pos_dic["width"],result_pos_dic["height"]) return result_pos_dic["x"], result_pos_dic["y"], result_pos_dic["width"],result_pos_dic["height"] else: runtime = time.time() - starttime if runtime >= waitfor: __logger.debug('get_element_rect [' + str(title) + '][' + str(selector) + ']') raise Au3ExecError('IE get_element_rect [' + str(title) + '][' + str(selector) + ']') __logger.debug('Attempt Failure - Wait for Attempt to Acquire:[' + str(title) + '][' + str(selector) + ']') time.sleep(TRY_INTERVAL) except Exception as e: raise e
def do_click_pos(win_title=None,win_text=None,control=None,button='left',curson='center',offsetX=0,offsetY=0,times=1, run_mode = 'unctrl',waitfor=WAIT_FOR): try: if run_mode == 'ctrl': return do_click_cs(win_title=win_title,win_text=win_text,control=control,button=button,times=times,waitfor=waitfor) __logger.debug('click control:[' + str(win_title) + '][' + str(control) + ']') if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) starttime = time.time() while True: X,Y = do_get_pos(win_title, control, curson, offsetX, offsetY) rst = _mouse_click_cs(button,X, Y, 2, times) if rst == 1: return rst else: runtime = time.time() - starttime if runtime >= waitfor: __logger.debug('Operation timeout') raise Au3ExecError('click control error:[' + str(win_title) + '][' + str(control) + ']') __logger.debug('click control error:[' + str(win_title) + '][' + str(control) + ']') time.sleep(TRY_INTERVAL) except Exception as e: raise e
def do_moveto_pos(win_title=None, win_text=None, control=None, curson='center', offsetX=0, offsetY=0, waitfor=WAIT_FOR): __logger.debug('Move mouse to control:[' + str(win_title) + '][' + str(control) + ']') try: if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) starttime = time.time() while True: X, Y = do_get_pos(win_title, control, curson, offsetX, offsetY) rst = _mouse_move_cs(X, Y, 2) if rst == 1: return rst else: runtime = time.time() - starttime if runtime >= waitfor: __logger.debug('Operation timeout') raise Au3ExecError('Move mouse error:[' + str(win_title) + '][' + str(control) + ']') __logger.debug('Move mouse error:[' + str(win_title) + '][' + str(control) + ']') time.sleep(TRY_INTERVAL) except Exception as e: raise e
def do_moveto_pos(win_title=None,title=None,url=None,selector=None,curson='center',offsetX=0,offsetY=0,waitfor=WAIT_FOR): __logger.debug('do_moveto_pos:[' + str(title) + '][' + str(selector) + ']') try: if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) starttime = time.time() while True: param = get_param(title, url, selector) result = dll.getElementRect(param) result_pos_json = string_at(result, -1).decode('utf-8') result_pos_dic = json.loads(result_pos_json) if int(result_pos_dic["retCode"]) == 1: X, Y = do_get_pos(result_pos_dic["x"], result_pos_dic["y"], result_pos_dic["width"], result_pos_dic["height"], curson, offsetX, offsetY) ics._mouse_move_cs(X, Y, 1) return True else: runtime = time.time() - starttime if runtime >= waitfor: __logger.debug('Operation timeout:[' + str(title) + '][' + str(selector) + ']') raise Au3ExecError('do_moveto_pos error:[' + str(title) + '][' + str(selector) + ']') __logger.debug('Attempt Failure - Wait for Attempt to Acquire:[' + str(title) + '][' + str(selector) + ']') time.sleep(TRY_INTERVAL) except Exception as e: raise e
def move_to_move(win_title="", x=0, y=0, str_xy="", waitfor=WAIT_FOR_IMG): __logger.debug(u"Ready to execute[move_to_move]") try: ''''如果指定窗口''' if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) msg = move_to_move_pack_au3(x, y, str_xy) tmp_au3_file_path = nit.gen_au3_file(msg) print(tmp_au3_file_path) status, error_string, stdout_string = nit.run_autoit(tmp_au3_file_path) nit.cleanup(tmp_au3_file_path) if status: '''程序执行错误''' __logger.error("aitprogram execution error") return False elif str(nit.get_cmd_message(stdout_string)) == "NO": '''autoit 执行返回结果为未找到''' __logger.error("move failure") return False return True except Exception as e: print(str(e), traceback.format_exc()) finally: __logger.debug(u"end execute[move_to_move]")
def img_exists_au3(win_title=None, img_res_path=None, image=None, waitfor=WAIT_FOR_IMG): __logger.debug(u"Ready to execute[img_exists]") is_exists = False try: ''''如果指定窗口''' if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) msg = get_img_exist_au3(img_res_path, image, waitfor) tmp_au3_file_path = nit.gen_au3_file(msg) status, error_string, stdout_string = nit.run_autoit(tmp_au3_file_path) nit.cleanup(tmp_au3_file_path) if status: '''program execution error''' __logger.error("aitprogram execution error") return False elif str(nit.get_cmd_message(stdout_string)) == "NO": '''autoit 执行返回结果为未找到''' __logger.error("ImageSearch image not found") return False return True except Exception as e: print(str(e), traceback.format_exc()) finally: __logger.debug(u"au3end execute[img_exists]" + str(is_exists))
def mouse_moveto(win_title=None, x=None, y=None, waitfor=WAIT_FOR): ''' 鼠标点击屏幕位置 ''' __logger.debug('mouse_moveto:[' + str(x) + '][' + str(y) + ']') try: ''''如果指定窗口''' if (win_title != None): '''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) starttime = time.time() while True: rst = _mouse_move_cs(x, y, 1) if rst == 1: return rst else: runtime = time.time() - starttime if runtime >= waitfor: __logger.debug('Operation timeout') raise Au3ExecError('mouse_moveto:[' + str(x) + '][' + str(y) + ']') __logger.debug('mouse_moveto:[' + str(x) + '][' + str(y) + ']') time.sleep(TRY_INTERVAL) except Exception as e: raise e
def get_select_items(win_title=None, id=None, waitfor=WAIT_FOR): """sap 得到下拉框全部选项""" try: __logger.debug('sap get select items :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) pass except Exception as e: raise e
def get_selected_item(win_title=None, id=None, waitfor=WAIT_FOR): """sap 得到下拉框当前选项""" try: __logger.debug('sap get selected item :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) item_text = element.value return item_text except Exception as e: raise e
def hotkey_send_cs(win_title=None, text=None, waitfor=WAIT_FOR): __logger.debug('hotkey send key:[win:' + str(win_title) + ']' + str(text)) try: ''''如果指定窗口''' if (win_title != None): ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) dll.AU3_Send(str(text), 0) except Exception as e: raise e
def set_select_item(win_title=None, id=None, item_text='', waitfor=WAIT_FOR): """sap 设置下拉框值""" try: __logger.debug('sap set select item :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) element.value = item_text except Exception as e: raise e
def get_text(win_title=None, id=None, row=None, col=None, waitfor=WAIT_FOR): """sap 获取文本""" try: __logger.debug('sap get text :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) if row != None and col != None and element.type == 'GuiShell': text = element.GetCellValue(row, col) else: text = element.text return text except Exception as e: raise e
def do_check(win_title=None, id=None, action="check", waitfor=WAIT_FOR): """sap checkbox""" try: __logger.debug('sap do check :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) if action == "check": element.selected = -1 else: element.selected = 0 except Exception as e: raise e
def get_column_value(win_title=None, id="", col=None, waitfor=WAIT_FOR): """sap shell表单获取整列""" try: __logger.debug('sap get column value :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) tableRowCount = element.RowCount column_list = [] for i in range(tableRowCount): cellvalue = element.GetCellValue(i, col) column_list.append(cellvalue) return column_list except Exception as e: raise e
def get_cell_value(win_title=None, id="", row=None, col=None, waitfor=WAIT_FOR): """sap shell表单获取单元格值""" try: __logger.debug('sap get cell value :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) cellvalue = element.GetCellValue(row, col) return cellvalue except Exception as e: raise e
def set_text(win_title=None, id=None, text=None, waitfor=WAIT_FOR, run_mode='noctrl'): """sap 设置文本""" try: __logger.debug('sap set text :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) text = encrypt.decrypt(str(text)) element.text = text except Exception as e: raise e
def do_click(win_title=None, id=None, row=None, col=None, button='left', curson='center', offsetX=0, offsetY=0, times=1, waitfor=WAIT_FOR, run_mode='unctrl'): """ sap 鼠标点击 """ try: __logger.debug('sap do click :' + str(id)) if run_mode == 'ctrl': element = get_element(id, waitfor) if row != None and col != None and element.type == 'GuiShell': element.setCurrentCell(row, col) if element.type == 'GuiButton': element.press() elif element.type == 'GuiRadioButton': element.select() elif element.type == 'GuiCheckBox': element.selected = not element.selected else: element.setFocus() else: if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) x, y = __get_element_pos(id, row, col, curson, offsetX, offsetY, waitfor) for i in range(times): if button == 'left': uiautomation.Win32API.MouseClick(x, y, 0) elif button == 'right': uiautomation.Win32API.MouseRightClick(x, y, 0) else: uiautomation.Win32API.MouseMiddleClick(x, y, 0) time.sleep(uiautomation.Win32API.GetDoubleClickTime() * 1.0 / 2000) except Exception as e: raise e
def img_click_au3(win_title=None, img_res_path=None, image=None, mouse_button="left", pos_curson="Center", pos_offsetY=0, pos_offsetX=0, times=1, image_size=None, waitfor=WAIT_FOR_IMG): __logger.debug(u"Ready to execute[img_click]") try: '''如果屏幕缩放比例不是100%''' if not __check_scale(): __logger.warning( u"Waring:The current screen zoom ratio is not 100%. Picture lookup may not be accurate." ) ''''如果指定窗口''' if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) msg = get_img_cli_au3(img_res_path, image, mouse_button, pos_curson, pos_offsetY, pos_offsetX, image_size, waitfor, times, 1) tmp_au3_file_path = nit.gen_au3_file(msg) status, error_string, stdout_string = nit.run_autoit(tmp_au3_file_path) nit.cleanup(tmp_au3_file_path) if status: '''程序执行错误''' __logger.error("Au3program execution error") return False elif str(nit.get_cmd_message(stdout_string)) == "NO": '''autoit 执行返回结果为未找到''' __logger.error("ImageSearch image not found") return False return True except Exception as e: print(str(e), traceback.format_exc()) return False finally: __logger.debug(u"end execute[img_click]")
def get_row_value(win_title=None, id="", row=None, waitfor=WAIT_FOR): """sap shell表单获取整行""" try: __logger.debug('sap get row value :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) element = get_element(id, waitfor) tableColumnCount = element.ColumnCount print(tableColumnCount) row_list = [] for i in range(tableColumnCount): cellvalue = element.GetCellValue(row, i) row_list.append(cellvalue) print(row_list) return row_list except Exception as e: raise e
def capture_image(win_title="", win_text="", in_img_path=None, left_indent=0, top_indent=0, width=0, height=0, waitfor=WAIT_FOR_IMG): __logger.echo_msg(u"Ready to execute[capture_image]") try: '''如果屏幕缩放比例不是100%''' if not __check_scale(): __logger.warning( u"Waring:The current screen zoom ratio is not 100%. Picture lookup may not be accurate." ) ''''如果指定窗口''' if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) ran_str = ''.join( random.sample(string.ascii_letters + string.digits, 8)) if in_img_path == None: # 当没有输入路径的时候 capture_image_path = nit.set_au3_file_res_path(__file__) in_img_path = capture_image_path + "\\temp\\" + ran_str + ".png" else: in_img_path = in_img_path + ran_str + ".png" right_indent = left_indent + width bottom_indent = top_indent + height bbox = (left_indent, top_indent, right_indent, bottom_indent) im = ImageGrab.grab(bbox) im.save(in_img_path) return in_img_path except Exception as e: raise e finally: __logger.echo_msg(u"end execute[capture_image]")
def do_moveto_pos(win_title=None, id=None, row=None, col=None, curson='center', offsetX=0, offsetY=0, waitfor=WAIT_FOR): ''' 鼠标移动到指定位置 ''' try: __logger.debug('sap do moveto :' + str(id)) if (win_title != None): if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) x, y = __get_element_pos(id, row, col, curson, offsetX, offsetY, waitfor) uiautomation.Win32API.MouseMoveTo(x, y) except Exception as e: raise e
def do_click_pos(win_title=None,title=None,url=None,selector=None,button='left',curson='center',offsetX=0,offsetY=0,times=1, waitfor=WAIT_FOR,run_mode='unctrl',scroll_view='no'): __logger.debug('do_click_pos:[' + str(title) + '][' + str(selector) + ']') try: if 'ctrl' == run_mode: do_click(title=win_title, url=url, selector=selector, waitfor=waitfor) else: if win_title != None and win_title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(win_title): iwin.do_win_activate(win_title=win_title, waitfor=waitfor) starttime = time.time() while True: param = get_param(title, url, selector) if scroll_view == 'yes': dll.doScrollIntoView(param) result = dll.getElementRect(param) result_pos_json = string_at(result, -1).decode('utf-8') result_pos_dic = json.loads(result_pos_json) if int(result_pos_dic["retCode"]) == 1: X, Y = do_get_pos(result_pos_dic["x"], result_pos_dic["y"], result_pos_dic["width"], result_pos_dic["height"], curson, offsetX, offsetY) ics._mouse_click_cs(button, X, Y, 1, times) return True else: runtime = time.time() - starttime if runtime >= waitfor: __logger.debug('Operation timeout:[' + str(title) + '][' + str(selector) + ']') raise Au3ExecError('do_click_pos error:[' + str(title) + '][' + str(selector) + ']') __logger.debug('Attempt Failure - Wait for Attempt to Acquire:[' + str(title) + '][' + str(selector) + ']') time.sleep(TRY_INTERVAL) except Exception as e: raise e
run_mode='unctrl'): __logger.debug('Click element operation:[' + str(title) + '][' + str(selector) + ']') try: if 'ctrl' == run_mode: click_element(program=program, title=title, className=className, selector=selector, async=async, waitfor=waitfor) else: if title != None and title.strip() != '': ''''如果窗口不活跃状态''' if not iwin.do_win_is_active(title): iwin.do_win_activate(win_title=title, waitfor=waitfor) starttime = time.time() while True: param = get_jsondata(program, title, className, selector, async=async) return_data = dll.getRectangleFromElem(param) return_data = reverse_data(return_data) if int(return_data["retCode"]) == 1: __logger.debug('Original position:' + str(return_data["x"]) + " y:" +