title: How to properly search for and randomly click multiple images with pyautogui?
body: <p>One of my initial python courses automates a simple cookie clicking game by using <code>pyautogui.click</code> at specific cordinates. I am trying to take this further by using the <code>locateonscreen</code> image functions and the <code>random</code> module to locate images and then randomly click within the images as I think this is more practical for my learning and more human-like.</p>
  <p>When the images are found - everything works. When the images are not found - I get an <code>AttributeError: 'NoneType' object has no attribute 'left'</code> because my box does not exist in that case. Im looking for help programming the logic to try to find and imagine and if it finds it randomly click it, otherwise try to find the next image.</p>
  <p>Here is what I have working when images exist:
  The while coordinates are to click a static location, then after the counter reaches a certain point look for and randomly click the images. Then return to the static location to continue clicking and loop.</p>
  <pre><code>import pyautogui as gui   
  import random             
  
  gui.PAUSE = 0.01
  
  gui.moveTo(x=383,y=576)
  counter = 1
  while gui.position() == (383,576):
      gui.click()
      counter += 1
      if counter % 300 == 0:
             
          def randomClick(box):
              x_click = int(random.uniform(box.left, box.left+box.width))
              y_click = int(random.uniform(box.top, box.top+box.height))
              return (x_click, y_click)
  
          Bank = gui.locateOnScreen('Bank.png')
          gui.moveTo(randomClick(Bank))
          gui.click() 
   
          def randomClick(box):
              x_click = int(random.uniform(box.left, box.left+box.width))
              y_click = int(random.uniform(box.top, box.top+box.height))
              return (x_click, y_click)
  
          Factory = gui.locateOnScreen('Factory.png')
          gui.moveTo(randomClick(Factory))
          gui.click() 
         
          def randomClick(box):
              x_click = int(random.uniform(box.left, box.left+box.width))
              y_click = int(random.uniform(box.top, box.top+box.height))
              return (x_click, y_click)
  
          Mine = gui.locateOnScreen('Mine.png')
          gui.moveTo(randomClick(Mine))
          gui.click()  
          
          def randomClick(box):
              x_click = int(random.uniform(box.left, box.left+box.width))
              y_click = int(random.uniform(box.top, box.top+box.height))
              return (x_click, y_click)
  
          Farm = gui.locateOnScreen('Farm.png')
          gui.moveTo(randomClick(Farm))
          gui.click()  
          
          def randomClick(box):
              x_click = int(random.uniform(box.left, box.left+box.width))
              y_click = int(random.uniform(box.top, box.top+box.height))
              return (x_click, y_click)
  
          Grandma = gui.locateOnScreen('Grandma.png')
          gui.moveTo(randomClick(Grandma))
          gui.click()   
      
          def randomClick(box):
              x_click = int(random.uniform(box.left, box.left+box.width))
              y_click = int(random.uniform(box.top, box.top+box.height))
              return (x_click, y_click)
  
          Cursor = gui.locateOnScreen('Cursor.png')
          gui.moveTo(randomClick(Cursor))
          gui.click()
          gui.moveTo(x=383,y=576)
  </code></pre>