sap可复用代码

发布时间 2023-12-08 11:17:26作者: ljn19961215

sap的登录操作可复用代码

class Chongxiao:

def __init__(self, username, password):
self.username = username
self.password = password

def PR3_Login(self,username, password):
"""
登录
:param username: 用户名
:param password: 密码
:return: session
"""
# username = sys_name
# password = sys_password
# 用户提供的账户密码
# username="LUOGQ223"
# password="lgq@12345"
print("用户名:", username, "密码:", password)
# 启动文件真是路径
true_path = 'C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\sapshcut.exe'
login_info = [true_path, '-system=PS7', '-client=800',
'-l=ZH',
'-user=%s' % username, '-pw=%s' % password]
if not os.path.exists(true_path):
# 启动文件的快捷方式路径
notepad_path = 'C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\sapshcut.exe.lnk'
wshell = win32com.client.Dispatch("WScript.Shell") # <COMObject WScript.Shell>
shortcut = wshell.CreateShortcut(notepad_path)
# 启动文件路径
login_info = [shortcut.TargetPath, '-system=PS7', '-client=800',
'-l=ZH',
'-user=%s' % username, '-pw=%s' % password]
subprocess.check_call(login_info)
try:
pythoncom.CoInitialize()
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
shell.SendKeys("^a", 0)
except Exception as e:
print(e)
# send_keys("%a")
time.sleep(2)
while True:
try:
SapGuiAuto = win32com.client.GetObject("SAPGUI")
application = SapGuiAuto.GetScriptingEngine
connection = application.Children(0)
session = connection.Children(0)
break
except Exception as e:
time.sleep(0.5)
session.findById("wnd[0]").maximize()
try:
session.findById("wnd[0]").maximize()
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").select()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
except Exception as e:
# print("error",e)
pass
window_caption = session.findById("wnd[0]").Text
print(window_caption)
if "口令不正确" in session.findById("wnd[0]/sbar/pane[0]").text:
print("用户名、密码错误")
return False, connection, ""
else:
return True, connection, session
def detect_app(self,username, password):
"""
检测sap是否已启动
:param username 用户名
:param password 密码
:return:
"""
try:
SapGuiAuto = win32com.client.GetObject("SAPGUI")
application = SapGuiAuto.GetScriptingEngine

connection = application.Children(0)
session_tmp = connection.Children(0)
session_tmp.createSession()
except:
erp_stat, connection, session_tmp = self.PR3_Login(username, password)

time.sleep(3)
session = connection.Children(connection.Children.count - 1)
return session

def run(self):
username = self.username
password = self.password
session = self.detect_app(username, password)



#下面就可以写录制的脚本