2016-09-05 116 views
0

我对编码相对比较陌生,当我在某个网页上时,我使得此程序重复鼠标笔画,以便可以自动执行此过程。在Python中停止迭代

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,但我无法弄清楚如何为我的脚本。

+0

你是什么意思“我也尝试使用CTRL + C添加KeyboardInterrupt”?你的意思是你不知道如何处理它并继续前进? https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python –

+0

你不需要添加任何东西到你的代码CTRL-C有时工作 – Julius

+0

,CTRL-C doesn不起作用,但是CTRL-Z可以,然后你可以用bash中的'kill%1'来杀死进程。将%1替换为您的%whateverthenumber您的工作,显然是 – Julius

回答

0

对于ctrl + C来说,活动窗口必须是运行python的终端。

我想,这可能是棘手的,当你有你的程序点击无处不在......

现在作出的回答值得你,我给你介绍网络浏览使用Python:http://selenium-python.readthedocs.io/getting-started.html#simple-usage

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

+0

这是一个很好的/有趣的猜测,但我完全不知道这个问题与网页浏览有什么关系。 –