Skip to content

Instantly share code, notes, and snippets.

Last active Jul 30, 2022
What would you like to do?
Robot for automation
import pyautogui
class Robot:
''' A base class to any robot. '''
def __init__(self):
''' Initializing the robot '''
self.pyautogui = pyautogui
self.ONE_SECOND = 1
self.MAX_RETRIES = 5
def write(self, text):
''' Writes some text on screen. '''
self.pyautogui.write(text, interval=0.25)
def click(self, x, y):
''' Moves to give position and clicks. '''
self.moveTo(x, y)
def moveTo(self, x, y):
''' Moves the mouse to location (x,y). '''
self.pyautogui.moveTo(x, y, self.ONE_SECOND)
def press(self, key):
''' Press the key on keyboard. '''
def pressHotKeys(self, keys):
if len(keys) == 2:
pyautogui.hotkey(keys[0], keys[1])
elif len(keys) == 3:
pyautogui.hotkey(keys[0], keys[1], keys[2])
print('err invalid hot key: ', keys)
def locateOnScreen(self, imagePath):
''' Locates an image on screen. '''
location = None
location = pyautogui.locateOnScreen(imagePath)
except Exception as err:
print (err)
return location
def locateImage(self, image):
counter = 0
locationFound = None
while counter < self.MAX_RETRIES:
counter += 1
locationFound = self.locateOnScreen(image)
if locationFound != None:
print('retry {} locate {} on screen '.format(counter, image))
return locationFound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment