【发布时间】:2018-02-21 13:36:47
【问题描述】:
我使用 SAPFEWSELib 来自动化 SAP。我从下拉菜单中按下按钮时遇到问题“我猜:)”。
此代码由 SAP 脚本记录自动生成。我需要在 C# 中重现它:
session.findById("wnd[0]/shellcont/shell/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
【问题讨论】:
我使用 SAPFEWSELib 来自动化 SAP。我从下拉菜单中按下按钮时遇到问题“我猜:)”。
此代码由 SAP 脚本记录自动生成。我需要在 C# 中重现它:
session.findById("wnd[0]/shellcont/shell/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
【问题讨论】:
解决了。
var ctrl = SapSession.ActiveWindow.FindById("wnd[0]/shellcont/shell/shellcont/shell", false);
var shellToolbarContextButton = ((GuiShell)ctrl);
var btnToolbarContextButton = shellToolbarContextButton as GuiGridView;
btnToolbarContextButton?.PressToolbarContextButton("&MB_EXPORT");
【讨论】:
实现相同结果的另一种方法:
首先,将第一个答案中的代码作为对我在此处的响应中使用的静态类的引用:How do I automate SAP GUI with c#。
其次,试试下面这行代码:
//Select Layout
GuiGridView guiGridView = (GuiGridView) SAPActive.SapSession.FindById("wnd[0]/usr/cntlCCONTAINER/shellcont/shell");
guiGridView.PressToolbarButton("&MB_VARIANT");
为了让生活更轻松并促进更高效的编码,我在 SAPActive 类中创建了一些基本方法,这些方法接受字符串 UI 路径并将其转换为所需的对象。 以下是一些示例:
/**
* Takes in a string UI path Id and returns a GuiTextField linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiTextField TextFieldPath(string path)
{
GuiTextField rtnField = (GuiTextField)SAPActive.SapSession.FindById(path);
return rtnField;
}
/**
* Takes in a string UI path Id and returns a GuiMenu linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiMenu MenuPath(string path)
{
GuiMenu rtnField = (GuiMenu)SAPActive.SapSession.FindById(path);
return rtnField;
}
/**
* Takes in a string UI path Id and returns a GuiFrameWindow linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiFrameWindow FrameWindowPath(string path)
{
GuiFrameWindow rtnField = (GuiFrameWindow)SAPActive.SapSession.FindById(path);
return rtnField;
}
/**
* Takes in a string UI path Id and returns a GuiButton linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiButton ButtonPath(string path)
{
GuiButton rtnField = (GuiButton)SAPActive.SapSession.FindById(path);
return rtnField;
}
/**
* Takes in a string UI path Id and returns a GuiGridView linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiGridView GridViewPath(string path)
{
GuiGridView rtnField = (GuiGridView) SAPActive.SapSession.FindById(path);
return rtnField;
}
现在,上面的代码可以改成如下:
GuiGridView guiGridView = SAPActive.GridViewPath("wnd[0]/usr/cntlCCONTAINER/shellcont/shell");
guiGridView.PressToolbarButton("&MB_VARIANT");
【讨论】: