ScreenshotNeo

BlogHow-to

How to Click with PyAutoGUI

Use PyAutoGUI to click at the current pointer or at screen coordinates. Learn the click options, safe coordinate checks, and fixes for clicks that miss.

By the ScreenshotNeo team1 October 20266 min read

Call pyautogui.click() to left-click at the current pointer position. Pass x and y to move to a screen coordinate and click there. Screen coordinates start at (0, 0) in the top-left of the primary screen; x increases to the right and y increases downward.

import pyautogui

pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.1

# Click where the pointer currently is.
pyautogui.click()

# Move to (100, 200) and click.
pyautogui.click(x=100, y=200)

Use coordinates only after confirming the target is on-screen and the intended window is in front. Keep PyAutoGUI’s fail-safe enabled so moving the pointer to the top-left corner can abort a script.

1. Install PyAutoGUI

Install the package with the Python environment you will use to run the script:

# Windows
py -m pip install pyautogui

# macOS or Linux
python3 -m pip install pyautogui

On Linux, PyAutoGUI’s installation guide lists additional prerequisites including scrot, python3-tk, and python3-dev. See the official installation instructions for platform-specific setup.

2. Make a basic click

Save this as click.py and run it from a graphical desktop session. Replace the sample coordinates with the location of a harmless target in the foreground application.

import pyautogui

# Moving the pointer to the upper-left corner raises FailSafeException.
pyautogui.FAILSAFE = True

# Pause after each public PyAutoGUI call to make actions observable.
pyautogui.PAUSE = 0.1

x, y = 100, 200
if not pyautogui.onScreen(x, y):
    raise ValueError(f"Coordinate {(x, y)} is outside the screen")

pyautogui.click(x=x, y=y)

click(x=..., y=...) moves to the supplied point and clicks in one call. To separate those actions, use moveTo() and then click(); this is helpful when you want to watch the pointer move before it clicks.

pyautogui.moveTo(100, 200, duration=0.2)
pyautogui.click()

A short duration makes movement visible while debugging. Very short moves may be treated as immediate according to PyAutoGUI’s MINIMUM_DURATION behavior.

3. Choose the mouse button and click count

click() accepts a button, a number of clicks, and the interval between repeated clicks. The button can be 'left', 'middle', or 'right'.

import pyautogui

# Explicit left click at the current pointer position
pyautogui.click(button='left')

# Right-click a coordinate
pyautogui.click(x=300, y=250, button='right')

# Double-click with a quarter-second between clicks
pyautogui.click(x=300, y=250, clicks=2, interval=0.25)

# Three middle clicks, spaced apart
pyautogui.click(button='middle', clicks=3, interval=0.2)

For readable intent, use the dedicated helpers:

pyautogui.doubleClick(x=300, y=250)
pyautogui.rightClick(x=300, y=250)
pyautogui.middleClick(x=300, y=250)

Use mouseDown() and mouseUp() when an interaction needs a custom press-and-release sequence, such as holding a button while moving:

pyautogui.moveTo(100, 200)
pyautogui.mouseDown(button='left')
pyautogui.moveTo(220, 200, duration=0.3)
pyautogui.mouseUp(button='left')

4. Check coordinates and screen bounds

The coordinate origin is the top-left corner of the primary screen. Inspect the pointer position and display dimensions before relying on fixed coordinates:

import pyautogui

print("Pointer:", pyautogui.position())
print("Screen size:", pyautogui.size())

x, y = 100, 200
print("On screen:", pyautogui.onScreen(x, y))

Use onScreen(x, y) to reject out-of-bounds positions before clicking. A coordinate can be in bounds and still point to the wrong control: window placement, display scaling, resolution, and application state all affect what occupies a point. Recheck coordinates whenever those conditions change.

5. Add timing and a reliable abort path

PyAutoGUI’s PAUSE setting adds a pause after each public call. A small pause gives the target application time to react and makes a sequence easier to observe. Set it near the start of the script:

import pyautogui

pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.2

pyautogui.click(x=100, y=200)

Keep FAILSAFE enabled. When it is True, moving the pointer to the upper-left corner raises pyautogui.FailSafeException, which can abort the program. Do not put the pointer in that corner during normal execution. If you need to handle the abort for cleanup, catch the exception around the automation sequence and let the program stop safely:

import pyautogui

pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.1

try:
    pyautogui.click(x=100, y=200)
except pyautogui.FailSafeException:
    print("Automation stopped by the PyAutoGUI fail-safe")

6. Common problems and fixes

Symptom Likely cause What to check or change
The click lands in the wrong place The coordinate was measured for a different resolution, scaling setting, or window position. Print pyautogui.position(), inspect pyautogui.size(), and confirm the target coordinate visually in the current desktop layout.
The click does nothing The target window may not have focus, the application may not be ready, or the session may restrict input control. Bring the intended window forward, add a modest PAUSE, and check platform permissions, display-server/session type, and whether the target runs elevated.
The script rejects a coordinate The coordinate falls outside the primary screen bounds. Check pyautogui.size() and pyautogui.onScreen(x, y); correct the coordinate before clicking.
FailSafeException is raised The pointer reached the upper-left fail-safe corner. Move the pointer away before continuing. Keep fail-safe enabled and handle the exception only if cleanup is needed.
The click happens before the interface is ready The application needs more time after a previous action or page change. Set a small pyautogui.PAUSE, or explicitly wait in your program for the application state before clicking. A fixed delay cannot guarantee that a slow or blocked interface is ready.
Installation fails on Linux System packages required by the desktop capture/input environment may be missing. Check the official PyAutoGUI installation guide for prerequisites such as scrot, python3-tk, and python3-dev.

7. Performance, reliability, and cost

A direct coordinate click is a short local input action. The time in a script usually comes from deliberate movement duration, pauses, and waiting for the target application to respond. Keep waits only as long as the interaction needs; a visible movement duration can help debug, while an immediate move is preferable when animation is unnecessary.

Fixed coordinates are sensitive to changes in screen size, scaling, window position, and page layout. Bounds checks catch points outside the screen, but cannot confirm that a button is still at an in-bounds point. For repeated or unattended workflows, confirm the application state and use an appropriate targeting strategy instead of assuming the interface never changes. The cited PyAutoGUI API documentation does not establish a benchmark that makes one targeting strategy universally more reliable.

PyAutoGUI is an installable Python package; the cited sources provide installation instructions but no usage pricing. The main operational cost is maintaining a compatible graphical session and handling the conditions that affect where input is sent.

8. Or skip the browser setup

If what you need is a screenshot of a web page rather than desktop mouse input, ScreenshotNeo provides a website screenshot API and MCP server. One GET request returns a PNG, JPEG, WebP, or PDF. See the ScreenshotNeo API docs for its parameters.

import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)

Cookie banners, newsletter popups, and chat widgets are removed before the shot. Bot checks, blank pages, timeouts, and failed loads are never billed, and the response identifies the page verdict and billing status in headers. An MCP server lets AI agents use take_screenshot, get_page_info, and capture_pdf. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots.

Create a free ScreenshotNeo account and get 1,000 screenshots a month with no card.

9. Frequently asked questions

Does PyAutoGUI click at the current pointer or move first?

With no coordinates, click() clicks at the current pointer. Supplying x and y makes it move to that point and click.

Can I click without moving the pointer?

Yes. Call pyautogui.click() without coordinates to click where the pointer already is.

How do I double-click?

Use pyautogui.doubleClick(x, y), or call pyautogui.click(x=x, y=y, clicks=2, interval=0.25).

What does a negative coordinate mean?

It is outside the primary screen coordinate range in the usual desktop setup. Check onScreen() before issuing the click.