How do you move the mouse with Playwright in Python?

I want to test mouse movement in Playwright Python. I tried using pyautogui to check the mouse position, but it always prints the same coordinates before and after page.mouse.move(100, 200).

I also tried adding page.mouse.down() and page.mouse.up() as shown in the docs, but the mouse still doesn’t move.

Example code:

import pytest
import pyautogui
from playwright.sync_api import sync_playwright

def test_simple_move():
    mouse_start_position = pyautogui.position()
    print(mouse_start_position)

    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False, slow_mo=10)
        page = browser.new_page()
        page.goto(r"http://www.uitestingplayground.com/")
        page.mouse.move(100,200)
        mouse_final_position = pyautogui.position()
        print(mouse_final_position)

How can I properly move the mouse using Playwright Python so that it reflects on the screen?

The behavior you’re seeing is expected: page.mouse.move() controls the mouse inside the browser context, not the actual system mouse.

That’s why pyautogui.position() doesn’t change it only tracks your OS mouse.

If you want to see movement in the browser, you can do something like this:

from playwright.sync_api import sync_playwright

with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto("http://www.uitestingplayground.com/drag")
    
    # Move mouse inside the browser
    page.mouse.move(100, 200)
    page.mouse.down()
    page.mouse.move(200, 300)
    page.mouse.up()

You’ll see a drag effect in the browser, no need for pyautogui.

If your goal is to move the actual mouse pointer on your desktop, Playwright won’t do that because it only simulates mouse events in the browser.

You’d need pyautogui or pynput:

import pyautogui

`# Move OS mouse to (100, 200)`
pyautogui.moveTo(100, 200, duration=1)  # duration for visible movement

Combine this with Playwright if needed, but keep in mind that Playwright won’t automatically sync with the OS mouse.

For most UI testing scenarios, you don’t need exact coordinates.

You can target elements directly:

from playwright.sync_api import sync_playwright

with sync_playwright() as pw:

   browser = pw.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto("http://www.uitestingplayground.com/drag")
    
    source = page.locator("#drag-box")
    source.hover()       # moves virtual mouse to element
    source.drag_to(page.locator("#drop-box"))  # simulates drag

This is usually more reliable than hardcoding (x, y) coordinates and ensures your tests work even if the UI layout changes.