python登录sap增强版 2021年7月22日 星期四

47 篇文章 3 订阅

print(‘count’, application.Children.Count)

#20210722新增
while not application.Children.Count:
    time.sleep(0.5)
    win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0)
    win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0)
    print('count 0 sleep 0.5')
    SapGuiAuto = win32com.client.GetObject("SAPGUI")
    application = SapGuiAuto.GetScriptingEngine

如果不能子进程数量为0,就延时0.5秒再点击一次登录按钮
在这里插入图片描述

def sap_login():
    '''
    登录SAP, 输入T-code, 返回获取的session
    :return: session
    '''

    kill_sap()

    sap_app = sap_path  # 您的saplogon程序本地完整路径
    subprocess.Popen(sap_app)

    time.sleep(int(wait_time))

    flt = 0
    while flt == 0:
        try:
            hwnd = win32gui.FindWindow(None, sap_version)
            flt = win32gui.FindWindowEx(hwnd, None, "Edit", None)
            print('hwnd', hwnd)
            print('flt', flt)
            time.sleep(3)
            print('---------------->')
        except:
            print('except============>')
            time.sleep(0.5)
    win32gui.SendMessage(flt, win32con.WM_SETTEXT, None, search_name)
    win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0)
    win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0)
    time.sleep(2)
    # 登录GUI界面

    time.sleep(0.3)
    dlg = win32gui.FindWindowEx(hwnd, None, "Button", None)
    win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0)
    win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0)


    time.sleep(2)
    SapGuiAuto = win32com.client.GetObject("SAPGUI")

    print(SapGuiAuto)
    print(type(SapGuiAuto))

    if not type(SapGuiAuto) == win32com.client.CDispatch:
        return
    application = SapGuiAuto.GetScriptingEngine
    print('application', application)
    print('count', application.Children.Count)
	
	#20210722新增
    while not application.Children.Count:
        time.sleep(0.5)
        win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0)
        win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0)
        print('count 0 sleep 0.5')
        SapGuiAuto = win32com.client.GetObject("SAPGUI")
        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)
    # print('type session', type(session))
    if not type(session) == win32com.client.CDispatch:
        connection = None
        application = None
        SapGuiAuto = None
        return

    session.findById("wnd[0]").maximize()
    session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "807"
    session.findById("wnd[0]/usr/txtRSYST-BNAME").text = username
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = password
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus()
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
    session.findById("wnd[0]").sendVKey(0)

    # 出现多用户登录
    # License Information for Multiple Logon

    multi_logon_text = session.findById("wnd[1]").text

    if 'License Information' in multi_logon_text:
        session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").select()
        session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").setFocus()
        session.findById("wnd[1]/tbar[0]/btn[0]").press()

    # 出现版权点击确定,没有版权提示直接进行下一步
    try:
        x = session.findById("wnd[1]").text
        # print(x)
        if 'Copyright' in x:
            session.findById("wnd[1]/tbar[0]/btn[0]").press()
    except:
        pass

    # 出现二级密码登录,不出现跳过
    try:
        session.findById("wnd[1]/usr/txtGS_OUT-ID").text = username2
        session.findById("wnd[1]/usr/pwdGS_OUT-PW").text = password2
        session.findById("wnd[1]/usr/pwdGS_OUT-PW").setFocus()
        session.findById("wnd[1]/usr/pwdGS_OUT-PW").caretPosition = 10
        session.findById("wnd[1]/usr/btnLOGIN").press()
    except:
        print('no second user')

    # 输入T-code
    session.findById("wnd[0]/tbar[0]/okcd").text = "/NME23N"
    session.findById("wnd[0]").sendVKey(0)

    print('session type', type(session))
    # with open('se', 'w') as f:
    #     f.write(session)
    #     print('write session')

    return session

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值