Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading