How to Click the Mouse in Python with the pyautogui Module



Python


In this article, we show how to click the mouse in Python using the pyautogui module.

A mouse click doesn't have to happen manually, as in I performing a mouse click or you performing it. We can perform a mouse click in our code to click anywhere on a screen.

pyautogui is a module that can do many different dynamic things, including sending virtual keypresses and mouse clicks to Windows.

With it, we can click an icon or anything on the screen.

So to click on a part of the screen of a computer, we use the pyautogui.click() function.

By default, this function uses the left mouse button and the click takes place wherever the mouse cursor is placed.

The code below clicks on the screen of the current position of the mouse cursor.



So the first thing we have to do is import the pyautogui module.

After this, we just call the pyautogui.click() function.

This code will produce a left mouse click at the current position of the mouse cursor.

If you want to produce a click anywhere other than the current position of the mouse cursor, then you have to specify the x- and y-coordinates of the screen where you want the mouse cursor to go and then click.

So in the code below, we click the screen at the location of 100 pixels to the left and 200 pixels down.



So now we produce a click at a precise location on the screen.

And this is how we can click a part of screen in Python with the pyautogui Module.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...