在python中停止迭代

时间:2016-09-05 03:17:16

标签: python loops break

我对编码比较陌生,当我在某个网页上时,我让这个程序重复鼠标笔划,这样就可以自动完成这个过程。

import pyautogui, time

inp = raw_input("Number input?")
iterations = raw_input("Iterations?")

def move(x, y):
    pyautogui.moveTo(x, y)

for i in range(int(iterations)):
    move(540,515)
    pyautogui.click()
    move(690,760)
    pyautogui.click()
    pyautogui.typewrite(inp)
    move(1200,790)
    pyautogui.click()
    move(1200,340)
    pyautogui.click()

如果我输入了太多的迭代,或者我需要在迭代完成之前再次控制我的鼠标,有没有办法停止循环/迭代?我尝试使用CTRL + BREAK,但它没有停止循环。我还尝试使用CTRL + C添加KeyboardInterrupt,但我无法弄清楚如何为我的脚本执行此操作。

1 个答案:

答案 0 :(得分:0)

要使ctrl + C正常工作,活动窗口必须是终端运行python的窗口。

我想当你的程序到处点击时可能会很棘手......

现在回答一下,我向您介绍使用python进行网页浏览:http://selenium-python.readthedocs.io/getting-started.html#simple-usage

这样你的程序就可以在后台运行,你可以随时杀死它。