在编写脚本 window 之前从 python 启动 SapGui 应用程序

Starting SapGui app from python before scripting window

我创建了一个 Python 应用程序来对 SAP GUI 进行 运行 功能测试,但我在启动 SAPGUI 会话时遇到了问题,我使用了通过脚本跟踪器捕获的脚本,但这发生在我获得 SAP 会话之前。

当我打开 SAP Logon 时,我得到一个 window 的环境列表,在这里我可以选择不同的服务器来使用,通常列表中的第一个环境是我需要的,所以我只需要发送 windows 一个输入以打开 SAP 正常会话并开始验证。

window 看起来像这样:

这是我用来打开 SAP 并启动第一个环境的代码:

os.startfile("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('{ENTER}')

然后我像这样创建一个 SAP 会话:

sapGuiAuto = win32com.client.GetObject("SAPGUI")
sapApplication = sapGuiAuto.GetScriptingEngine
time.sleep(1)
sapConnection = sapApplication.Children(0)
session = sapConnection.Children(0)

第一段代码随机运行,就像三次中的一次。

我已经阅读了一些关于 com 对象的内容,我认为我可以从一开始就使用 win32com.client.GetObject("SAPGUI"),但我一直在尝试做这些事情但没有成功:

测试 1

sapGuiAuto = win32com.client.GetObject("SAPGUI")
sapGuiAuto.SendKeys('{ENTER}')

测试 2

sapGuiAuto = win32com.client.GetObject("SAPGUI")
sapApplication = sapGuiAuto.GetScriptingEngine
time.sleep(1)
sapConnection = sapApplication.Children(0)
session = sapConnection.Children(0)
session.findById("wnd[0]").sendVKey(0)

有没有比使用 win32com.client.Dispatch("WScript.Shell") 更好的启动 SAP GUI 的方法? SAP Logon 是否有不同的 ProgId?

谢谢

我不知道 Python 但你可以尝试以下方法:

'Here comes the full name of the connection from SAP Logon
myConnection = "DE2 [erpdd...]" 
os.startfile("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")
shell = win32com.client.Dispatch("WScript.Shell")
time.sleep(4)

sapGuiAuto = win32com.client.GetObject("SAPGUI")
sapApplication = sapGuiAuto.GetScriptingEngine
sapConnection = sapApplication.openconnection(myConnection)
session = sapConnection.Children(0)
session.findById("wnd[0]").maximize
'start authenticating
...

问候,ScriptMan