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?

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.