Skip to content

Instantly share code, notes, and snippets.

@samueltcsantos
Last active September 10, 2023 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samueltcsantos/18bbee3c269b329b47eaabe739934014 to your computer and use it in GitHub Desktop.
Save samueltcsantos/18bbee3c269b329b47eaabe739934014 to your computer and use it in GitHub Desktop.
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)
pyautogui.click()
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. '''
self.pyautogui.press(key)
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])
else:
print('err invalid hot key: ', keys)
def locateOnScreen(self, imagePath):
''' Locates an image on screen. '''
location = None
try:
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:
break
else:
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