def AddString(self, text): print("ComboBox: Adding string %r" % text) ### gui.SendMessage(self.hwnd, wc.CB_ADDSTRING, 0, text)
def show_window(handle): #最大化窗口 if win32gui.IsIconic(handle): win32gui.SendMessage(handle, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0) time.sleep(0.2)
def _SendSci(self, msg, wparam=0, lparam=0): return win32gui.SendMessage(self.hwnd, msg, wparam, lparam)
def _click_login_button(self): time.sleep(1) input_hwnd = win32gui.GetDlgItem(self.login_hwnd, 0x1) win32gui.SendMessage(input_hwnd, win32con.BM_CLICK, None, None)
def hide_window(handle): #最小化窗口 win32gui.SendMessage(handle, win32con.WM_SYSCOMMAND, win32con.SC_MINIMIZE, 0)
def _set_trade_mode(self): input_hwnd = win32gui.GetDlgItem(self.login_hwnd, 0x4f4d) win32gui.SendMessage(input_hwnd, win32con.BM_CLICK, None, None)
def _set_login_password(self, password): time.sleep(0.5) input_hwnd = win32gui.GetDlgItem(self.login_hwnd, 0x5534) win32gui.SendMessage(input_hwnd, win32con.WM_SETTEXT, None, password)
def close(self): """ Closes the window. """ win32gui.SendMessage(self.hwnd, win32con.WM_CLOSE, 0, 0)
#上传图纸零件 tuzhi_upload = driver.find_element( By.XPATH, '//*[@id="panel1"]/div[2]/div[1]/div[1]/div/div[1]/label') tuzhi_upload.click() time.sleep(3) # 使用win32gui dialog = win32gui.FindWindow('#32770', u'打开') # 对话框 ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None) ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None) Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None) # 上面三句依次寻找对象,直到找到输入框Edit对象的句柄 button = win32gui.FindWindowEx(dialog, 0, 'Button', None) # 打开按钮Button win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, tuzhiPath) # 往输入框输入绝对地址 win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) # 按button time.sleep(5) #上传图纸清单 liebiao_upload = driver.find_element( By.CSS_SELECTOR, '#panel1 > div.panel-body.row.mt-lg.form-horizontal.in.collapse > div:nth-child(2) > div.col-md-12.row.mb-lg > div > div.box-placeholder.my-drop-zone.bg-user > label' ) liebiao_upload.click() # print('qingdan!!!!!!!!!!!!!!!!!!!!!!!!!!') time.sleep(3) #同上 dialog = win32gui.FindWindow('#32770', u'打开') # 对话框 ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None)
from selenium import webdriver import win32gui import win32con import time dr = webdriver.Firefox() dr.get('http://www.sucaijiayuan.com/api/demo.php?url=/demo/20150128-1') dr.switch_to.frame('iframe') upload = dr.find_element_by_class_name('filePicker') upload.click() time.sleep(1) # win32gui dialog = win32gui.FindWindow('#32770', u'文件上传') ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None) ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None) Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None) button = win32gui.FindWindowEx(dialog, 0, 'Button', None) win32gui.SendMessage(Edit, win32con.WM_SETTEXT, 0, '"d:\\baidu.py" "d:\\upload.py" "d:\\1.html"') win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) print dialog print Edit print button print dr.find_element_by_id('status_info').text dr.quit()
print("窗口不对!") return None os.startfile(p2p_path) childs = [] time.sleep(0.5) root_desktop_hd = None p2p_hd = win32gui.FindWindowEx(root_desktop_hd, 0, 0, pdg2pic_str) # 第一下我们点选的按钮没有名字 fst_btn_hd = get_hd_from_child_hds(p2p_hd, 3, expect_name="") win32api.PostMessage(fst_btn_hd, win32con.BM_CLICK) time.sleep(0.5) p2p_hd = win32gui.FindWindowEx(root_desktop_hd, 0, 0, pdg2pic_str) names = [win32gui.GetWindowText(each) for each in childs] win32gui.EnumChildWindows(p2p_hd, lambda hwnd, param: param.append(hwnd), childs) names = [win32gui.GetWindowText(each) for each in childs] list1_hd = names[18] win32gui.SendMessage(list1_hd, win32con.BM_CLICK) for idx, name in enumerate(names): print(name, "\t\t", idx + 1)
# encoding=utf-8
def close_window(self): """Close the current window""" if self._handle is not None: win32gui.SendMessage(self._handle, win32con.WM_CLOSE, 0, 0)
def _sendCommand(self, id): win32gui.SendMessage(self._hwnd, self.APPCOMMAND, 0, id)
def _sendNotifyMessage(hwnd, nofifyMessage): '''Send a notify message to a control.''' win32gui.SendMessage( win32gui.GetParent(hwnd), win32con.WM_COMMAND, _buildWinLong(nofifyMessage, win32api.GetWindowLong(hwnd, win32con.GWL_ID)), hwnd)
def set_text(self, text): win32gui.SendMessage(self.hwnd, win32con.WM_SETTEXT, None, text)
def write(self, data): length = win32gui.GetWindowTextLength(self.handle) win32gui.SendMessage(self.handle, win32con.EM_SETSEL, length, length) win32gui.SendMessage(self.handle, win32con.EM_REPLACESEL, False, data)
def send_enter(self): win32gui.SendMessage(self.hwnd, win32con.WM_CHAR, 0x0D, 0)
def _set_login_name(self, user): time.sleep(0.5) input_hwnd = win32gui.GetDlgItem(self.login_hwnd, 0x5523) win32gui.SendMessage(input_hwnd, win32con.WM_SETTEXT, None, user)
def click(self): x = 5 y = 5 pos = makeParam(x, y) win32gui.SendMessage(self.hwnd, win32con.WM_LBUTTONDOWN, 1, pos) win32gui.SendMessage(self.hwnd, win32con.WM_LBUTTONUP, 1, pos)
def _input_login_verify_code(self, code): input_hwnd = win32gui.GetDlgItem(self.login_hwnd, 0x56b9) win32gui.SendMessage(input_hwnd, win32con.WM_SETTEXT, None, code)
def set_text(self, text): message = ctypes.create_unicode_buffer(text) message_ref = ctypes.addressof(ctypes.byref(message)._obj) win32gui.SendMessage(self.handle, win32con.WM_SETTEXT, 0, message_ref)
import pdb import time import win32api import win32gui import win32con win = win32gui.FindWindow(None, '1111.txt - 记事本') tid = win32gui.FindWindowEx(win, None, 'Edit', None) win32gui.SendMessage(tid, win32con.WM_SETTEXT, None, '你好hello word!') win32gui.PostMessage(tid, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) #pdb.set_trace() win32gui.SetForegroundWindow(win) win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0) win32api.keybd_event(79, 0, 0, 0) # 79:O win32api.keybd_event(79, 0, win32con.KEYEVENTF_KEYUP, 0) win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0) ''' #win32gui.SetForegroundWindow(win) win32gui.PostMessage(tid, win32con.WM_KEYDOWN, 17, 0) time.sleep(1) #win32api.SendMessage(win, win32con.WM_CHAR, 79, 0) win32gui.PostMessage(tid, win32con.WM_KEYDOWN, 79, 0) time.sleep(0.0001) #win32gui.PostMessage(tid, win32con.WM_KEYUP, 79, 0) time.sleep(1) #win32gui.PostMessage(tid, win32con.WM_KEYUP, 17, 0) time.sleep(0.5) ''' print("%x" % tid) print("%x" % win)
def getWindowText(hwnd): buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) buf = win32gui.PyMakeBuffer(buf_size) win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buf) return str(buf)
def mouse_scroll(handle): #模拟鼠标滚轮 win32gui.SendMessage(handle, win32con.WM_MBUTTONDOWN, win32con.MK_MBUTTON, 0) time.sleep(0.1) win32gui.SendMessage(handle, win32con.WM_MBUTTONUP, 0, 0)
def clickClient(handle,pos): coordinate = win32api.MAKELONG(pos[0], pos[1]) win32gui.SendMessage(handle,win32con.WM_ACTIVATE,win32con.WA_ACTIVE,0) #这里应该是控制前台还是后台模拟 win32api.SendMessage(handle,win32con.WM_LBUTTONDOWN,win32con.MK_LBUTTON,coordinate) win32api.SendMessage(handle,win32con.WM_LBUTTONUP,win32con.MK_LBUTTON,coordinate)
def OnNotify(self, hwnd, msg, wparam, lparam): hwndFrom, idFrom, code = win32gui_struct.UnpackWMNOTIFY(lparam) #print "OnNotify code=0x%x (0x%x, 0x%x)" % (code, wparam, lparam) if code == commctrl.NM_SETFOCUS: # Control got focus - Explorer may not know - tell it if self.browser is not None: self.browser.OnViewWindowActive(None) # And do our menu thang self._OnActivate(shellcon.SVUIA_ACTIVATE_FOCUS) elif code == commctrl.NM_KILLFOCUS: self._OnDeactivate() elif code == commctrl.NM_DBLCLK: # This DblClick implementation leaves a little to be desired :) # It demonstrates some useful concepts, such as asking the # folder for its context-menu and invoking a command from it. # However, as our folder delegates IContextMenu to the shell # itself, the end result is that the folder is opened in # its "normal" place in Windows explorer rather than inside # our shell-extension. # Determine the selected items. sel = [] n = -1 while 1: n = win32gui.SendMessage(self.hwnd_child, commctrl.LVM_GETNEXTITEM, n, commctrl.LVNI_SELECTED) if n==-1: break sel.append(self.children[n][-1:]) print("Selection is", sel) hmenu = win32gui.CreateMenu() try: # Get the IContextMenu for the items. inout, cm = self.folder.GetUIObjectOf(self.hwnd_parent, sel, shell.IID_IContextMenu, 0) # As per 'Q179911', we need to determine if the default operation # should be 'open' or 'explore' flags = shellcon.CMF_DEFAULTONLY try: self.browser.GetControlWindow(shellcon.FCW_TREE) flags |= shellcon.CMF_EXPLORE except pythoncom.com_error: pass # *sob* - delegating to the shell does work - but lands us # in the original location. Q179911 also shows that # ShellExecuteEx should work - but I can't make it work as # described (XP: function call succeeds, but another thread # shows a dialog with text of E_INVALID_PARAM, and new # Explorer window opens with desktop view. Vista: function # call succeeds, but no window created at all. # On Vista, I'd love to get an IExplorerBrowser interface # from the shell, but a QI fails, and although the # IShellBrowser does appear to support IServiceProvider, I # still can't get it if 0: id_cmd_first = 1 # TrackPopupMenu makes it hard to use 0 cm.QueryContextMenu(hmenu, 0, id_cmd_first, -1, flags) # Find the default item in the returned menu. cmd = win32gui.GetMenuDefaultItem(hmenu, False, 0) if cmd == -1: print("Oops: _doDefaultActionFor found no default menu") else: ci = 0, self.hwnd_parent, cmd-id_cmd_first, None, None, 0, 0, 0 cm.InvokeCommand(ci) else: rv = shell.ShellExecuteEx( hwnd = self.hwnd_parent, nShow=win32con.SW_NORMAL, lpClass="folder", lpVerb="explore", lpIDList=sel[0]) print("ShellExecuteEx returned", rv) finally: win32gui.DestroyMenu(hmenu)
def sendWhatsApp(message_to, message, attachments=[]): # By this you will give the location where to search your target or contact # So it will specify the place of message box on top # and than search inside that your contact name if found than move ahead try: # new_chat = web_driver.find_element_by_id('input-chatlist-search') inp_xpath = '/html/body/div[1]/div/div/div[3]/div/div[1]/div/label/input' new_chat = web_driver.find_element_by_xpath(inp_xpath) except: return False, 'No se encontró el placeholder para la búsqueda del destinatario' new_chat.send_keys(message_to, Keys.ENTER) time.sleep(2) # Test if destinatary was found inp_xpath = '/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[2]/div/div[2]' hwnd = web_driver.find_element_by_xpath(inp_xpath) if hwnd == []: return False, 'Destinatario no encontrado en la lista de contactos' # Add the attachments to the message for attachment in attachments: # Press Attach button button = web_driver.find_elements_by_xpath( '//*[@id=\"main\"]/header/div[3]/div/div[2]/div/span') button[0].click() # Presiona botón 'Fotos y videos' (li[1]) 1. Fotos y videos, 2. Cámara, 3. Documento, 4. Contacto inp_xpath = '//*[@id=\"main\"]/header/div[3]/div/div[2]/span/div/div/ul/li[1]/button' button = web_driver.find_elements_by_xpath(inp_xpath) button[0].click() # Loop until Open dialog is displayed (my Windows version is in Spanish) hdlg = 0 while hdlg == 0: hdlg = win32gui.FindWindow(None, "Abrir") try: win32gui.SetForegroundWindow(hdlg) except: pass time.sleep(1) # second # Set filename and press Enter key hwnd = win32gui.FindWindowEx(hdlg, 0, 'ComboBoxEx32', None) hwnd = win32gui.FindWindowEx(hwnd, 0, 'ComboBox', None) hwnd = win32gui.FindWindowEx(hwnd, 0, 'Edit', None) win_attachment = os.path.normcase(attachment) #.replace('/', '\\') # Send folder and press Save button win32gui.SendMessage(hwnd, win32con.BM_CLICK, None, None) win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, None, os.path.dirname(win_attachment)) btnOpen = win32gui.FindWindowEx(hdlg, 0, 'Button', '&Abrir') win32gui.SendMessage(btnOpen, win32con.BM_CLICK, None, None) time.sleep(40) # second # Send filename and press Save button win32gui.SendMessage(hwnd, win32con.BM_CLICK, None, None) win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, None, os.path.basename(win_attachment)) btnOpen = win32gui.FindWindowEx(hdlg, 0, 'Button', '&Abrir') win32gui.SendMessage(btnOpen, win32con.BM_CLICK, None, None) time.sleep(25) # second # Press send button inp_xpath = '//*[@id=\"app\"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span[2]/div/div/span' button = web_driver.find_elements_by_xpath(inp_xpath) button[0].click() # if len(attachments) > 0: # # Wait 'till file is loaded (wait 'till download button is shown) # inp_xpath = '//*[@id="main"]/div[2]/div/div/div[2]/div[9]/div/div[1]/div/a/div[2]/div[3]/span' # web_driver.find_elements_by_xpath(inp_xpath) # Write aditional message inp_css = 'div._3FeAD._1PRhq' input_box = web_driver.find_elements_by_css_selector(inp_css) input_box[0].send_keys(message, Keys.ENTER) # Press Send message button # button = web_driver.find_elements_by_css_selector('button._3M-N-') # button[0].click() time.sleep(1) # second return True, ''
# finding only Chrome's windows. This works for both the browser # and content_shell. # # It seems safest to send the WM_CLOSE messages after discovering # all of the sub-process's windows. def find_chrome_windows(hwnd, hwnds): try: _, win_pid = win32process.GetWindowThreadProcessId(hwnd) if (pid == win_pid and win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.GetClassName(hwnd).lower().startswith( app_name)): hwnds.append(hwnd) except pywintypes.error, e: error_code = e[0] # Some windows may close after enumeration and before the calls above, # so ignore those. if error_code != winerror.ERROR_INVALID_WINDOW_HANDLE: raise return True hwnds = [] win32gui.EnumWindows(find_chrome_windows, hwnds) if hwnds: for hwnd in hwnds: win32gui.SendMessage(hwnd, win32con.WM_CLOSE, 0, 0) return True else: logging.info('Did not find any windows owned by target process') return False
def winRightClick(self, _hwnd, _x, _y): client_pos = [_x, _y] tmp=win32api.MAKELONG(client_pos[0],client_pos[1]) win32gui.SendMessage(_hwnd, win32con.WM_ACTIVATE,win32con.WA_ACTIVE,0) win32gui.SendMessage(_hwnd, win32con.WM_LBUTTONDOWN,win32con.MK_LBUTTON,tmp) win32gui.SendMessage(_hwnd, win32con.WM_LBUTTONUP,0,tmp)