一些基础的操作SAP的Code

Open SAP

# Open SAP
def open_sap():
    try:
        Application(backend="win32").start(sap_path, timeout=60)
        log.info("Open SAP Successful")
    except:
        log.error("Open SAP Failed")

 

Enter AP1 and logon

# Enter AP1 then Logon
def enter_sap_system(username, password, sid):
    try:
        # 登录AP1 (sid)
        log.info("Enter SAP system")
        app = Application(backend="win32").connect(path=sap_path)
        app.window(title='SAP Logon Pad 770').wait('exists ready', timeout=60)
        sap_window = app.window(title='SAP Logon Pad 770')
        sap_window.child_window(class_name="Edit").set_focus()
        sap_window.child_window(class_name="Edit").set_text(sid)
        sap_window.child_window(class_name="Edit").set_focus()
        send_keys("{ENTER}")
        time.sleep(2)
        sap_window.child_window(class_name="Edit").set_focus()
        sap_window.set_focus()
        send_keys("+{ENTER}")
        log.info("Enter sap system successful")
        session = ""
        time.sleep(2)
        # 连接SAP
        while True:
            try:
                pythoncom.CoInitialize()
                SapGuiAuto = win32com.client.GetObject("SAPGUI")
                application = SapGuiAuto.GetScriptingEngine
                if not type(application) == win32com.client.CDispatch:
                    SapGuiAuto = None
                connection = application.Children(0)
                if not type(connection) == win32com.client.CDispatch:
                    application = None
                    SapGuiAuto = None
                session = connection.Children(0)
                if not type(session) == win32com.client.CDispatch:
                    connection = None
                    application = None
                    SapGuiAuto = None
                break
            except:
                log.warning("Connect failed, please wait 3 seconds")
                time.sleep(3)

        # 登录
        try:
            session.findById("wnd[0]/usr/txtRSYST-BNAME").text = username
            session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = password
            session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "EN"
            session.findById("wnd[0]/tbar[0]/btn[0]").press()
        except:
            log.warning("Login failed")

        # 登录冲突,踢人
        try:
            session.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").select()
            session.findById("wnd[1]/tbar[0]/btn[0]").press()
        except:
            pass

        # 有时会弹出确认框,点击确认即可
        try:
            time.sleep(3)
            session.findById("wnd[1]/tbar[0]/btn[0]").press()
        except:
            pass

        log.info("login successful, user:" + username)
        return session
    except:
        log.info("Enter SAP System failed")

退出SAP

def exit_after_n_second(n):
    time.sleep(n)
    # 关闭SAP
    __close_sap_window_by_class_name('SAP_FRONTEND_SESSION')
    __close_sap_window_by_title('SAP Logon Pad 770')

 

原文地址:http://www.cnblogs.com/kykstyle/p/16813241.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性