Hi there,
I've just started using python to automate SAP GUI scripting (have been using VBA for that). However, it wouldnt work when I was trying to extract data from SAP.
In this case I was trying to get the value contract number in T-code VA03-Additional Data B.
I suppose this is probably little things i have completely ignored but would be good if someone can point it out...
It seems this particular field cannot be found by Python. However i tried the same method and it worked with VBA very well.
Here's the code:
# Import modules
import sys
import win32com.client
# SAP function
def sapconnection():
try:
SapGuiAuto = win32com.client.GetObject("SAPGUI")
if not type(SapGuiAuto) == win32com.client.CDispatch:
return
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
session = connection.Children(0)
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
return
# Check VC number
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/tbar[0]/okcd").text = "/nva03"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtVBAK-VBELN").text = soNbr #HERE IS WHERE I PUT THE SO
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/mbar/menu[2]/menu[1]/menu[14]/menu[1]").select()
#HERE IS WHERE IT THROWS AN ERROR
vc = session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\13/ssubSUBSCREEN_BODY:SAPMV45A:4312/sub8309:SAPMV45A:8309/txtVBAK-ZZCON").text
except:
#print(sys.exc_info()[0])
print(sys.exc_info())
finally:
session = None
connection = None
application = None
SapGuiAuto = None
#main
if __name__ == "__main__":
sapconnection()
Request clarification before answering.
vincentthelibra
Hello,
your code works so far, I tried it on my test system. But there is an error in your string.
"wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\13/ssubSUBSCREEN_BODY:SAPMV45A:4312/sub8309:SAPMV45A:8309/txtVBAK-ZZCON"The string contains a backslash and it is necessary to escape them. Try it on this way...
"wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\\13/ssubSUBSCREEN_BODY:SAPMV45A:4312/sub8309:SAPMV45A:8309/txtVBAK-ZZCON"...and let us know your result.
Best regards
Stefan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks this is exactly the mistake...I knew it would be a very basic mistake. 😞
| User | Count |
|---|---|
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.