SAP Login failed

Hi @Allan_Zimmermann
I tried your suggestion and the issue persisted, but I found a solution that worked for me. I then created a python script to execute the login and it works fine.

@UnyBots
Hopefully this code can help you out a bit. This code is a Python script that interacts with the SAP GUI application using the pywin32 module: pip install pywin32

import win32com.client
import sys
import subprocess
import time

class SapGui():
    def __init__(self):

        self.path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe" # Path to saplogon instalation folder
        subprocess.Popen(self.path)
        time.sleep (2)

        self.SapGuiAuto = win32com.client.GetObject("SAPGUI")
        if not isinstance(self.SapGuiAuto, win32com.client.CDispatch):
            return

        application = self.SapGuiAuto.GetScriptingEngine
        self.connection = application.OpenConnection("SAP PROD", True) # Name of your SAP connection

        time.sleep(3)
        self.session = self.connection.Children(0)
        self.session.findById("wnd[0]").maximize

    def sapLogin(self):
        try:
            # Set the text of the SAP GUI element with the ID for login
            self.session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "***" # Client
            self.session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "***" # User
            self.session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "***" # Password
            self.session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "***" # Language
            self.session.findById("wnd[0]").sendVKey(0)

            # check a element you want to verify login status
            elemExist = self.session.findById("wnd[0]/tbar[0]/btn[15]")
            if elemExist is not None:
                # Resize the working pane of the SAP GUI window
                self.session.findById("wnd[0]").resizeWorkingPane(100, 20, False)

                # Login was successful
                # Perform further actions

            else:
                # Login failed or window text does not match and Handle the failure case
                # Display an error message
                print("Login failed or window text does not match")

                # Close the SAP GUI connection
                self.connection.CloseSession('ses[0]')

                # Terminate the SAP GUI application
                application = self.SapGuiAuto.GetScriptingEngine()
                application.Quit()

                # Raise an exception or return an error code if necessary
                raise Exception("Login failed!")
        except:
            print(sys.exc_info())

sap_gui = SapGui()
sap_gui.sapLogin()

This code demonstrates a basic interaction with the SAP GUI using the win32com.client module in Python. It starts the SAP GUI, performs a login operation, and performs some actions if the login is successful.

@Allan_Zimmermann If I want to create this as an agent, is it possible? (after some improvments)

2 Likes