def Main(): sap_app = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe" subprocess.Popen(sap_app) time.sleep(1) flt = 0 while flt == 0: try: hwnd = win32gui.FindWindow(None, "SAP Logon 750") flt = win32gui.FindWindowEx(hwnd, None, "Edit", None) except Exception as e: time.sleep(0.5) win32gui.SendMessage(flt, win32con.WM_SETTEXT, None, "IR2-ERP Integration UAT") win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0) win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0) time.sleep(0.1) dlg = win32gui.FindWindowEx(hwnd, None, "Button", None) # 登陆(0) win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0) win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0) SapGuiAuto = win32com.client.GetObject("SAPGUI") if not type(SapGuiAuto) == win32com.client.CDispatch: return application = SapGuiAuto.GetScriptingEngine if not type(application) == win32com.client.CDispatch: SapGuiAuto = None return connection = application.Children(0) if not type(connection) == win32com.client.CDispatch: application = None SapGuiAuto = None return time.sleep(2) flag = 0 while flag == 0: try: session = connection.Children(0) flag = 1 except: time.sleep(0.5) if not type(session) == win32com.client.CDispatch: connection = None application = None SapGuiAuto = None return session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "WANG" # 此次放入您的SAP登陆用户名 session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "Wxz" # 此次放入您的SAP登陆密码 session.findById("wnd[0]").sendVKey(0) if __name__ == "__main__": Main()
标黄的地方是需要自己改的