如何在python方法上创建循环

2024-09-30 00:25:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本,使用一个测试框架在这些标签上重新创建用户的操作。我正在使用以下命令:

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

一直到listBox.LabelButton5。我如何遍历它以最小化Python上的冗余?你知道吗

我试过了

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

在Python中,这在语法上是不正确的。什么是合适的方法?你知道吗


Tags: 用户命令脚本框架标签keysenterlistbox
1条回答
网友
1楼 · 发布于 2024-09-30 00:25:35

列一个按钮列表或元组;遍历:

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

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

我怀疑你有一些重复的按钮创建过程;这是一个很好的时间把它们都塞进一个列表。你知道吗

相关问题 更多 >

    热门问题