0

I'm writing a script using a testing framework to recreate a user's action on these labels. I'm using the following:

listBox.LabelButton.Keys("[Enter]")
winword.WaitProcess("winword", 2000)
listBox.LabelButton2.Keys("[Enter]")
winword.WaitProcess("winword", 2000)
listBox.LabelButton3.Keys("[Enter]")
winword.WaitProcess("winword", 2000)

all the way down to listBox.LabelButton5. How can I iterate through this in order to minimize this redundancy on Python?

I tried

listbox.LabelButton.Keys("[Enter]")
winword.WaitProcess("winword",2000)
for i in range (2,6):
   listBox.LabelButton + str(i).Keys("[Enter]")
   winword.WaitProcess("winword", 2000)

This is not syntactically correct in Python. What is the appropriate approach?

Prune
  • 76,765
  • 14
  • 60
  • 81
Caliman
  • 202
  • 2
  • 4
  • 20
  • maybe duplicate of [Create and use multiple variables in a loop in Python](https://stackoverflow.com/questions/23323725/create-and-use-multiple-variables-in-a-loop-in-python)? – Patrick Artner Nov 27 '18 at 18:49
  • What are you using? Python 3.x or Python 2.7 - and why did you tag the respective other one you are not using? – Patrick Artner Nov 27 '18 at 18:53

1 Answers1

3

Make a list or tuple of your buttons; iterate through that:

button_list = [
    listBox.LabelButton,
    listBox.LabelButton2,
    listBox.LabelButton3,
    ...
]

for button in button_list:
    button.Keys("[Enter]")
    winword.WaitProcess("winword", 2000)

I suspect that you have some sort of repetitive button creation process; that's a good time to stuff them all into a list.

Prune
  • 76,765
  • 14
  • 60
  • 81