User = session.findById("wnd[0]/usr")
for i in range(User.Children.Count):
print('i',i)
name = User.Children(i).Name
print('name', name)
全部代码
import win32com.client, wmi, os, json
import win32gui, win32con, time, subprocess
import sqlite3, xlrd
from datetime import datetime
today = datetime.today().strftime('%Y%m%d')
# 从excel获取SAP登录信息
def get_params():
data = xlrd.open_workbook('config.xls')
sheet_name = 'Sheet1'
table = data.sheet_by_name(sheet_name)
# print('sheet1', table.nrows)
path = table.cell(1, 0).value
version = table.cell(1, 1).value
search_name = table.cell(1, 2).value
username = table.cell(1, 3).value
password = table.cell(1, 4).value
username2 = table.cell(1, 5).value
password2 = table.cell(1, 6).value
query_date = table.cell(1, 7).value
wait_time = table.cell(1, 8).value
# print('path', path)
# print('version', version)
# print('search_name', search_name)
# print('username', username)
# print('password', password)
# print('username2', username2)
# print('password2', password2)
# print('query_date', query_date)
# print('wait_time', wait_time)
# return path, version, search_name, username, password, username2, password2, wait_time
return path, version, search_name, username, password, username2, password2, query_date, wait_time
# 杀掉sap进程
def kill_sap():
c = wmi.WMI()
for process in c.Win32_Process(name="saplogon.exe"):
print(process.ProcessId, process.Name)
process.Terminate()
def sap_login_test_env():
'''
登录SAP, 输入T-code, 返回获取的session
:return: session
'''
sap_path = r"C:\Users\Public\SapGui\saplogon.exe"
sap_version = "SAP Logon 740"
search_name = "tuser"
username = "TUSER"
password = "FOXCONN8"
sap_app = sap_path # 您的saplogon程序本地完整路径
subprocess.Popen(sap_app)
time.sleep(5)
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)
time.sleep(3)
win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0)
win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0)
time.sleep(0.2)
win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0)
win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0)
time.sleep(0.3)
win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0)
win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0)
time.sleep(4)
# 登录GUI界面
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)
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-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)
try:
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()
except:
pass
session.findById("wnd[0]/tbar[0]/okcd").text = "va01"
session.findById("wnd[0]").sendVKey(0)
return session
sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params()
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)
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
def get_23n_info(session, po):
'''
从 /NME23N
获取料号、数量、版次
:param session:
:param po:
:return:
'''
# 输入T-code
session.findById("wnd[0]/tbar[0]/okcd").text = "/NME23N"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/tbar[1]/btn[17]").press()
session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").text = "4500144386"
session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").caretPosition = 10
session.findById("wnd[1]").sendVKey(0)
time.sleep(2)
item_list = []
n = m = 0
while True:
item = {}
#把底部item展开,数据变为三行
try:
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB3:SAPLMEVIEWS:1100/subSUB1:SAPLMEVIEWS:4002/btnDYN_4000-BUTTON").press()
except:
pass
quantity = session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-MENGE[9,{str(n)}]").text
# 有时会变成SAPLMEGUI:0013或0020
# session.findById(
# "wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-MENGE[9,0]").setFocus()
name = get_name()
batch = session.findById(
f"wnd[0]/usr/sub{name}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-CHARG[18,{str(n)}]").text
material = session.findById(
f"wnd[0]/usr/sub{name}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EMATN[4,{str(n)}]").text
if material in str(item_list):
# print(material)
# print(str(item_list))
# print('已经最后一个')
break
if len(material) > 0:
item['material'] = material
item['quantity'] = quantity
item['batch'] = batch
item_list.append(item)
# print('material', material)
# print('quantity', quantity)
# print('batch', batch)
n += 1
m += 1
# print('n--------->', n)
if m // 3:
session.findById(
f"wnd[0]/usr/sub{name}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").verticalScrollbar.position = m
# print('m--------->', m)
n = 0
if not material:
break
print(len(item_list))
return item_list,name
def get_name():
'''
获取 SUB0:SAPLMEGUI:0016
:return:
'''
User = session.findById("wnd[0]/usr")
for i in range(User.Children.Count):
# print('i', i)
name = User.Children(i).Name
# print('name --->', name)
return name
def to_21n(session, po, item_list, name):
#测试多料号输入
# item_list = item_list * 6
'''
往21n写入信息
:param session:
:param po:
:param item_list:
:return:
'''
session.findById("wnd[0]/tbar[0]/okcd").text = "/NME21N"
session.findById("wnd[0]").sendVKey(0)
name = get_name()
session.findById(
f"wnd[0]/usr/sub{name}/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/cmbMEPO_TOPLINE-BSART").key = "ZZNB"
name = get_name()
session.findById(f"wnd[0]/usr/sub{name}/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/ctxtMEPO_TOPLINE-SUPERFIELD").text = "Z775O1005"
session.findById(f"wnd[0]/usr/sub{name}/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT9/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221/ctxtMEPO1222-EKORG").text = "SGGD"
session.findById(f"wnd[0]/usr/sub{name}/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT9/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221/ctxtMEPO1222-EKGRP").text = "001"
session.findById(f"wnd[0]/usr/sub{name}/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT9/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221/ctxtMEPO1222-BUKRS").text = "SG22"
#
# if len(item_list) >= 9:
# #写入数据翻页
# session.findById(
# "wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").verticalScrollbar.position = 9
m = 9
n = 8
p = 0
for j in range(0, len(item_list) // 8 + 1):
if j == 0:
print('第一次9个',len(item_list[0:m]))
for i in range(len(item_list[0:m])):
session.findById(
f"wnd[0]/usr/sub{name}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EMATN[4,{i}]").text = \
item_list[p]['material']
session.findById(
f"wnd[0]/usr/sub{name}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-MENGE[9,{i}]").text = \
item_list[p]['quantity']
session.findById(
f"wnd[0]/usr/sub{name}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-NAME1[7,{i}]").text = "SGGD"
session.findById(
f"wnd[0]/usr/sub{name}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-CHARG[18,{i}]").text = \
item_list[p]['batch']
p = p + 1
else:
print('翻页前')
#写入数据翻页
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").verticalScrollbar.position = 9 * j
try:
#翻页后输入框发生变化,点击head恢复原样
if j == 1:
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB1:SAPLMEVIEWS:1100/subSUB1:SAPLMEVIEWS:4000/btnDYN_4000-BUTTON").press()
except:
print('已经点击过头部了')
#继续翻页进行数据录入
# 点入焦点
# session.findById(
# f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-NETPR[13,3]").caretPosition = 10
print('p--------->', p)
# if p > 16:
# break
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").verticalScrollbar.position = 16
for i in range(p-8, p):
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").verticalScrollbar.position = i
print('next page',i)
if i == p -1:
# 时常出现点击不灵敏,连击五次使其到达指定位置
for i in range(5):
session.findById(
"wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211").verticalScrollbar.position = p - 1
print('翻页')
# time.sleep('翻页')
# if p > 16:
# time.sleep('翻页')
# time.sleep(17)
print('翻页数量', 9 *j)
#time.sleep('翻页后')
# print('翻页')
for i in range(len(item_list[m: m + n])):
i = i + 1
print('i p',i, p)
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EMATN[4,{i}]").text = \
item_list[p]['material']
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-MENGE[9,{i}]").text = \
item_list[p]['quantity']
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-NAME1[7,{i}]").text = "SGGD"
session.findById(
f"wnd[0]/usr/sub{get_name()}/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-CHARG[18,{i}]").text = \
item_list[p]['batch']
p = p + 1
m = m + n
if not len(item_list[m: m + n]):
break
# for i in range(len(item_list)):
# session.findById(
# f"wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EMATN[4,{i}]").text = item_list[i]['material']
# session.findById(
# f"wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-MENGE[9,{i}]").text = item_list[i]['quantity']
# session.findById(
# f"wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-NAME1[7,{i}]").text = "SGGD"
# session.findById(
# f"wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-CHARG[18,{i}]").text = item_list[i]['batch']
#保存按钮
session.findById("wnd[0]/tbar[0]/btn[11]").press()
#确认保存
session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press()
#other purchase order
session.findById("wnd[0]/mbar/menu[0]/menu[0]").select()
# 获取单号
new_po = session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").text
print('new_po', new_po)
#退出小窗
session.findById("wnd[1]").sendVKey(0)
if __name__ == '__main__':
# kill_sap()
# session = sap_login()
sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params()
session = sap_login()
for i in range(1):
item_list, name = get_23n_info(session, 1)
to_21n(session, 1, item_list, name)
参考代码
Set User = session.findById("wnd[0]/usr")
For i = 0 To User.Children.Count - 1
Name = User.Children(CInt(i)).Name
If Left(Name, 15) = "SUB0:SAPLMEGUI:" Then
Exit For
End If
Next
MsgBox Name
MsgBox session.findById("wnd[0]/usr/sub" & Name & "/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL").Children.Count
参考
https://answers.sap.com/questions/684650/vb-scripting-what-is-the-rule-behind-the-numbering.html