title: Automatically Click Mouse at Random location using pynput (for a web browser game)
body: <p><strong>Info.</strong>  </p>
  
  <p>Hey there :)<br>
  I'm learning Python for a few weeks now, and I just started with some small projects. Now I'm building a script to automate an webbrowser game. The script sends out a few "expeditions" which give me more rescources inside the game. The script is already working but i would like to improve it. If you have any tips i would love to hear them. </p>
  
  <p><strong>Question.</strong><br>
  <strong>I'm using pynput and mouse.position = () for an exact location to click. Is there a way to make the click random inside a certain area?</strong> Because normal person woudn't always click in the same location. </p>
  
  <p>like click at a random location between these positions:
  mouse.position (2000, 500)
  mouse.position (3000, 1000)</p>
  
  <p><strong>My script.</strong></p>
  
  <pre><code>import pynput, time, random, sys
  from pynput.keyboard import Key, Controller as KeyboardController
  from pynput.mouse import Button, Controller as MouseController
  timeDelay = random.randrange(2, 4)
  
  
  def locateogame():
      #---------------------------------------------&gt; Getting to Ogame.nl
      mouse = MouseController()
      keyboard = KeyboardController()
      mouse.position = (2392, 48)
      mouse.click(Button.left, 1)
      time.sleep(timeDelay)
      keyboard.type("Ogame.nl")
      keyboard.press(Key.enter)
      keyboard.release(Key.enter)
      #login to account and universe.
      time.sleep(timeDelay)
      mouse.position = (2343, 564)
      mouse.click(Button.left, 1)
  
  def p1():
      #---------------------------------------------&gt; Locate to planet 1
      mouse = MouseController()
      time.sleep(timeDelay)
      mouse.position = (3054, 298)
      mouse.click(Button.left, 1)
  
  def p2():
     #---------------------------------------------&gt; Locate to planet 2
      #Locate to 5:352:8
      mouse = MouseController()
      time.sleep(timeDelay)
      mouse.position = (3060, 382)
      mouse.click(Button.left, 1)
  
  def p3():
      #---------------------------------------------&gt; Locate to planet 3
      #Locate to 5:353:7 
      mouse = MouseController()
      time.sleep(timeDelay)
      mouse.position = (3074, 438)
      mouse.click(Button.left, 1)
  
  def p4():
      #---------------------------------------------&gt; Locate to planet 4
      #Locate to 5:353:8 
      mouse = MouseController()
      time.sleep(timeDelay)
      mouse.position = (3073, 481)
      mouse.click(Button.left, 1)
  
  def p5():
      #---------------------------------------------&gt; Locate to planet 5
      #Locate to 4:32:8 
      mouse = MouseController()
      time.sleep(timeDelay)
      mouse.position = (3099, 538)
      mouse.click(Button.left, 1)
  
  def Sending():
  #---------------------------------------------&gt; This will do all the clicking to send my ships
      mouse = MouseController()
      keyboard = KeyboardController()
      #Select Fleet from menu
      time.sleep(timeDelay)
      mouse.position = (2254, 493)
      mouse.click(Button.left, 1)
      #select "Expeditie" Fleet
      time.sleep(timeDelay)
      mouse.position = (2670, 694)
      mouse.click(Button.left, 1)
      #Expedition
      time.sleep(timeDelay)
      mouse.position = (2598, 743)
      mouse.click(Button.left, 1)
      time.sleep(timeDelay)
      mouse.position = (2954, 678)
      mouse.click(Button.left, 1)
      #select slot 16
      time.sleep(timeDelay)
      mouse.position = (2796, 434)
      mouse.click(Button.left, 1)
      keyboard.type("16")
      keyboard.press(Key.enter)
      keyboard.release(Key.enter)
      #expeditie button
      time.sleep(timeDelay)
      mouse.position = (2408, 378)
      mouse.click(Button.left, 1)
      #send Fleet
      time.sleep(timeDelay)
      mouse.position = (2862, 711)
      mouse.click(Button.left, 1)
  
  
  ##---------------------------------------------&gt; Start of the script.
  locateogame()
  
  fns = [p1, p2, p3, p4, p5]
  from random import choice
  choice(fns)()
  
  Sending()
  
  </code></pre>
  
  <p>Thanks for your time and have a nice day!</p>