mvinegret
mvinegret

Reputation: 31

Can I infinitely loop a pyautogui function using time.sleep()?

I'm very new to python and I'm just trying to create a simple mouse moving+clicking script to remove some of my sent LinkedIn connection requests. I'm trying to make it loop infinitely but I just can't figure it out :(

import pyautogui
from time import sleep

time.sleep(2)

pyautogui.PAUSE = 2.5

pyautogui.moveTo(1171, 458)
pyautogui.click()
pyautogui.moveTo(1123, 403)
pyautogui.click()

I tried to do the following, but it doesn't execute the code at all and I need to stop the kernel:

import pyautogui
from time import sleep

while True:
    sleep(2)

pyautogui.PAUSE = 2.5

pyautogui.moveTo(1171, 458)
pyautogui.click()
pyautogui.moveTo(1123, 403)
pyautogui.click()

Upvotes: 0

Views: 2007

Answers (1)

ZiyadCodes
ZiyadCodes

Reputation: 438

You can do this using a while loop like this:

from time import sleep
import pyautogui

pyautogui.PAUSE = 2.5

while True:
    sleep(2)

    pyautogui.moveTo(1171, 458)
    pyautogui.click()
    pyautogui.moveTo(1123, 403)
    pyautogui.click()

If you don't want the script to sleep for 2 seconds every time, you can move the sleep(2) above the while True: and remove the spaces behind the sleep(2)

Upvotes: 1

Related Questions