def TestObjectFromWindow(): # Check we can use ObjectFromLresult to get the COM object from the # HWND - see KB Q249232 # Locating the HWND is different than the KB says... hwnd = win32gui.FindWindow('IEFrame', None) for child_class in [ 'TabWindowClass', 'Shell DocObject View', 'Internet Explorer_Server' ]: hwnd = win32gui.FindWindowEx(hwnd, 0, child_class, None) # ack - not working for markh on vista with IE8 (or maybe it is the # lack of the 'accessibility' components mentioned in Q249232) # either way - not working! return # But here is the point - once you have an 'Internet Explorer_Server', # you can send a message and use ObjectFromLresult to get it back. msg = win32gui.RegisterWindowMessage("WM_HTML_GETOBJECT") rc, result = win32gui.SendMessageTimeout(hwnd, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000) ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0) doc = Dispatch(ob) # just to prove it works, set the background color of the document. for color in "red green blue orange white".split(): doc.bgColor = color time.sleep(0.2)
import win32com import win32com.client #spyxx.exe #Chrome #p1 = win32gui.FindWindow("CefWebViewWnd", "微信") #p2 = win32gui.FindWindowEx(p1,0,"CefBrowserWindow","") #p3 = win32gui.FindWindowEx(p2,0,"Chrome_WidgetWin_0","") #p4 = win32gui.FindWindowEx(p3,0,"Chrome_RenderWidgetHostHWND","Chrome Legacy Window") ##IE,只能在IE版本下调试通过,Chrome下不行 p0 = win32gui.FindWindow("IEWebViewWnd", "微信") p1 = win32gui.FindWindowEx(p0, 0, "ActiveXWnd", "UIActiveX") p2 = win32gui.FindWindowEx(p1, 0, "Shell Embedding", "") p3 = win32gui.FindWindowEx(p2, 0, "Shell DocObject View", "") p4 = win32gui.FindWindowEx(p3, 0, "Internet Explorer_Server", "") print("p0=", p0, "p1=", p1, "p2=", p2, "p3=", p3, "p4=", p4) msg = win32gui.RegisterWindowMessage('WM_HTML_GETOBJECT') ret, result = win32gui.SendMessageTimeout(p4, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000) #result = win32gui.SendMessage(p4, msg, 0, 0) print("result=", result) ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0) doc = win32com.client.dynamic.Dispatch(ob) print("url=", doc.url, "title=", doc.title) #doc.all['id'].click()
def loginIE(login, password): #pythoncom.CoInitializeEx(0) # not use this for multithreading #Connect to internet explorer server instance mainHwnd = win32gui.FindWindow('ADALWebBrowserHost', '') if mainHwnd: ieServers = [] win32gui.EnumChildWindows(mainHwnd, getIEServer, ieServers) if len(ieServers) > 0: ieServer = ieServers[0] msg = win32gui.RegisterWindowMessage('WM_HTML_GETOBJECT') ret, result = win32gui.SendMessageTimeout( ieServer, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000) ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0) doc = win32com.client.dynamic.Dispatch(ob) print("connected to IE server") try: win32gui.SetForegroundWindow(mainHwnd) except: print("couldn't SetForegroundWindow 1") return False #for i in range(2): #Make sure that we've got all elements loaded page_type = "" login_not_ready = True submit_not_ready = True password_not_ready = True while (login_not_ready or submit_not_ready or password_not_ready): #Get elements from document try: for el in doc.all: #Try is needed because not all elements have both name and type fields try: if el.name == "loginfmt": login_element = el login_not_ready = False print("received login element") if el.type == "submit": submit_element = el submit_not_ready = False print("received btn element") if el.name == "passwd": password_element = el password_not_ready = False except: print("element has no name attribute") #sleep(0.1) continue except: print("doc isn't loaded yet") return False sleep(0.1) #Figure out what page is loaded if password_element.className == "moveOffScreen": page_type = "login_page" elif login_element.className == "moveOffScreen": page_type = "password_page" if page_type == "login_page": #Paste login login_element.focus() login_element.value = login submit_element.style.backgroundColor = "#000000" submit_element.focus() submit_element.blur() submit_element.click() wait_password_page_to_load(login_element) elif page_type == "password_page": #Paste password password_element.focus() password_element.value = password submit_element.style.backgroundColor = "#000000" submit_element.focus() submit_element.blur() submit_element.click() print("ok") return True else: print("page_type unspecified") else: print("No IE server found") return False