Python Script For Loop

Question:

So I have these python program:

for index, row in data.iterrows():

    self.session.findById("wnd[0]/tbar[1]/btn[17]").press()
    self.session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").text = row.Document
    self.session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").caretPosition = 10
    self.session.findById("wnd[1]/tbar[0]/btn[0]").press()
    self.session.findById("wnd[0]/tbar[1]/btn[7]").press()
    self.session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/btnEDITFILTER").press()
    self.session.findById("wnd[1]/usr/subSUB_DYN0500:SAPLSKBH:0600/btnAPP_WL_SING").press()
    self.session.findById("wnd[1]/usr/subSUB_DYN0500:SAPLSKBH:0600/btn600_BUTTON").press()
    self.session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").text = row.Position
    self.session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-HIGH").text = row.Position
    self.session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-HIGH").setFocus
    self.session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-HIGH").caretPosition = 3
    self.session.findById("wnd[2]/tbar[0]/btn[0]").press()
    self.session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-NETPR[10,0]").text = row.New_Price
    self.session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-NETPR[10,0]").setFocus
    self.session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-NETPR[10,0]").caretPosition = 14
    self.session.findById("wnd[0]/tbar[0]/btn[11]").press()

And I want to run this line:

self.session.findById("wnd[0]/tbar[1]/btn[7]").press()

only once in the loop.

How do I run it once only? (only that line)

Asked By: Marcelo Estrada

||

Answers:

Like Olvin Roght said, try moving the code outside the loop because that specific line has nothing to do with the loop in the first place. You are not using the row or the index for that line. So it makes no sense to keep it inside the loop unless you aren’t telling us something.

However, to answer your question, you could create a boolean variable outside the loop to track if the loop has been run and do your action on the first try. Something like this:

firstRun = True
for index, row in data.iterrows():
    self.session.findById("wnd[0]/tbar[1]/btn[17]").press()
    self.session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").text = row.Document
    self.session.findById("wnd[1]/usr/subSUB0:SAPLMEGUI:0003/ctxtMEPO_SELECT-EBELN").caretPosition = 10
    self.session.findById("wnd[1]/tbar[0]/btn[0]").press()
    if firstRun: # THIS IS TRUE on the first run
        self.session.findById("wnd[0]/tbar[1]/btn[7]").press() # Do the click once.
        firstRun = False # Set it to False so that subsequent runs are not treated as first run.
    # Run All other clicks and stuff in your loop as usual.
Answered By: whiplash
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.