Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Use Playwright’s mouse or locator API to trigger the interaction, then use page.waitForFunction() to wait for a truthy condition that proves the page reached the state you expect. For a regular button click, prefer a locator; use page.mouse when coordinates or a deliberate move/press/release sequence are part of the test. Start the wait before triggering the action if the page could update immediately.

What waitForFunction does—and what it does not do

page.waitForFunction(pageFunction, arg?, options?) evaluates a function in the page and waits until its result is truthy. It does not click, hover, or otherwise generate a mouse event. Your test must perform the interaction separately, and the function should describe an observable result of that interaction, such as an attribute changing, a message appearing, or a menu becoming visible. See the Playwright Page API.

For example, after a save click, wait for the application’s saved state rather than merely waiting for a fixed duration. This ties the test to what the user needs to happen and avoids guessing how long the update takes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Wait for a state change after a click

Set up the wait before the action. If the application changes state quickly, starting the wait afterward risks missing a transient condition or creating confusing timing behavior. The predicate below checks a stable data attribute; replace the selector and expected value with a state your application exposes.

#1 Best Overall
Sale
Logitech M185 Compact Ambidextrous 2.4 GHz Wireless Mouse - Swift Grey
  • Compact Mouse: With a comfortable and contoured shape, this Logitech ambidextrous wireless mouse feels great in either right or left hand and is far superior to a touchpad
  • Durable and Reliable: This USB wireless mouse features a line-by-line scroll wheel, up to 1 year of battery life (2) thanks to a smart sleep mode function, and comes with the included AA battery
  • Universal Compatibility: Your Logitech mouse works with your Windows PC, Mac, or laptop, so no matter what type of computer you own today or buy tomorrow your mouse will be compatible
  • Plug and Play Simplicity: Just plug in the tiny nano USB receiver and start working in seconds with a strong, reliable connection to your wireless computer mouse up to 33 feet / 10 m (5)
  • Better than touchpad: Get more done by adding M185 to your laptop; according to a recent study, laptop users who chose this mouse over a touchpad were 50% more productive (3) and worked 30% faster (4)
const changed = page.waitForFunction(() =>
  document.querySelector('[data-status]')?.getAttribute('data-status') === 'ready',
  null,
  { timeout: 5_000 }
);

await page.getByRole('button', { name: 'Save' }).click();
await changed;

This example uses a locator because the target is an ordinary button. Locator clicks perform actionability checks, which helps ensure the element is a usable target rather than merely dispatching a click at a point. The timeout is explicit so a missing state does not leave this wait running indefinitely. The documented waitForFunction timeout default is 0 (no timeout); page, context, or test configuration can affect effective timeouts, so choose a finite value appropriate to your suite.

Use page.mouse when coordinates or input sequencing matter

Use page.mouse when the test specifically concerns a pointer position, a canvas or other coordinate-driven surface, or an explicit series of pointer actions. Mouse coordinates are CSS pixels relative to the main-frame viewport. A mouse is associated with a page, and mouse.click(x, y) is a shortcut for moving to the point, pressing, and releasing. mouse.move(x, y, { steps }) can emit interpolated mousemove events on the way to the destination. The Mouse API documentation describes these methods and coordinates; that URL is the Next documentation channel, so confirm the installed Playwright version’s API if version-specific behavior matters.

const changed = page.waitForFunction(() =>
  document.querySelector('[data-status]')?.getAttribute('data-status') === 'ready',
  null,
  { timeout: 5_000 }
);

await page.mouse.move(320, 240);
await page.mouse.click(320, 240);
await changed;

The click point must correspond to the intended target in the current viewport. If a layout shift, scroll, or responsive breakpoint changes the target’s position, hard-coded coordinates can hit the wrong area. Prefer a locator when coordinates themselves are not under test.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Wait for a hover-driven change

For a hover test, move the mouse to the target and wait for the resulting application state. If the test is about a CSS hover effect, assert a visible or computed outcome that accurately represents the behavior being tested; if it is about application logic, wait for that logic’s observable state.

Rank #2
Sale
Logitech G305 Lightspeed Wireless Gaming Mouse - Black
  • The next-generation optical HERO sensor delivers incredible performance and up to 10x the power efficiency over previous generations, with 400 IPS precision and up to 12,000 DPI sensitivity
  • Ultra-fast LIGHTSPEED wireless technology gives you a lag-free gaming experience, delivering incredible responsiveness and reliability with 1 ms report rate for competition-level performance
  • G305 wireless mouse boasts an incredible 250 hours of continuous gameplay on just 1 AA battery; switch to Endurance mode via Logitech G HUB software and extend battery life up to 9 months
  • Wireless does not have to mean heavy, G305 lightweight mouse provides high maneuverability coming in at only 3.4 oz thanks to efficient lightweight mechanical design and ultra-efficient battery usage
  • The durable, compact design with built-in nano receiver storage makes G305 not just a great portable desktop mouse, but also a great laptop travel companion, use with a gaming laptop and play anywhere
const menuOpened = page.waitForFunction(() =>
  document.querySelector('[data-menu]')?.getAttribute('aria-expanded') === 'true',
  null,
  { timeout: 5_000 }
);

await page.mouse.move(320, 240);
await menuOpened;

For an ordinary element-targeted interaction, a locator’s hover method can be more robust than manually calculating coordinates. Use the low-level mouse API when the path or pointer location is the subject of the test.

Choose the right API: locator, mouse, or dispatchEvent

Approach Best for What it exercises
Locator action Normal user-like interaction with a known element Element-targeted actions with Playwright actionability checks
page.mouse Coordinate-based behavior or controlled move/press/release sequences Low-level pointer input in viewport CSS-pixel coordinates
dispatchEvent('click') Testing code that responds to a directly dispatched DOM event DOM event handling, not whether a user-like pointer could reach the element

dispatchEvent('click') can dispatch an event regardless of visibility. It is not interchangeable with a locator click or a physical-style mouse interaction when the test needs to verify visibility, actionability, or pointer reachability. The Page API documents direct event dispatch as well as locator-oriented click guidance.

Wait for an event or wait for the resulting state?

These are different test questions. If you need to know whether an event itself occurred, use an event-waiting API that matches that event. If you need to know whether clicking or hovering caused the page to reach a state, use a predicate that checks that state. A generic delay does not establish either fact.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

waitForFunction runs its predicate in the page context and resolves when the result is truthy, returning a JSHandle for that value. You can pass an argument to the predicate when a test needs to supply a value rather than hard-code it:

Rank #3
Sale
Logitech M185 Compact Ambidextrous Wireless Mouse with Rubber Grips - Blue
  • Compact Mouse: With a comfortable and contoured shape, this Logitech ambidextrous wireless mouse feels great in either right or left hand and is far superior to a touchpad
  • Durable and Reliable: This USB wireless mouse features a line-by-line scroll wheel, up to 1 year of battery life (2) thanks to a smart sleep mode function, and comes with the included AA battery
  • Universal Compatibility: Your Logitech mouse works with your Windows PC, Mac, or laptop, so no matter what type of computer you own today or buy tomorrow your mouse will be compatible
  • Plug and Play Simplicity: Just plug in the tiny nano USB receiver and start working in seconds with a strong, reliable connection to your wireless computer mouse up to 33 feet / 10 m (5)
  • Better than touchpad: Get more done by adding M185 to your laptop; according to a recent study, laptop users who chose this mouse over a touchpad were 50% more productive (3) and worked 30% faster (4)
const expected = 'ready';
const ready = page.waitForFunction(
  status => document.querySelector('[data-status]')?.getAttribute('data-status') === status,
  expected,
  { timeout: 5_000 }
);

await page.getByRole('button', { name: 'Save' }).click();
await ready;

Use a page function for a condition that must be checked in the page. page.evaluate() is a different tool: it runs JavaScript in the page context and automatically waits if the function returns a Promise. It is appropriate for page-context work, but it is not the same as waitForFunction’s repeated truthy-predicate wait. See the Evaluating JavaScript guide.

Write reliable predicates

  • Check a real outcome. Prefer a state tied to the application behavior—such as an attribute or visible result—over an arbitrary elapsed time.
  • Use stable selectors. A test-specific attribute or accessible state is less fragile than relying on incidental text or styling that may change.
  • Return a boolean or other deliberate truthy value. A predicate that always returns a truthy object will resolve immediately; make sure the condition actually distinguishes the desired state.
  • Set a finite timeout where appropriate. The documented default is no timeout, and effective behavior may also be affected by page or context configuration.
  • Keep the target and predicate aligned. Ensure the action is expected to cause the exact state being checked; a correctly executed click cannot make an unrelated predicate succeed.

Troubleshooting common failures

The wait times out

Check that the action reached the intended element and that the predicate matches the state the application really produces. A selector may be wrong, the expected attribute value may differ, or the state may be conditional. If using coordinates, confirm the target’s location in the main-frame viewport after scrolling and layout changes.

The wait resolves immediately

Inspect the predicate for a condition that is already true or an expression that returns a truthy value unconditionally. For example, returning an element object only checks whether that element exists, not whether it has reached the desired state. Compare the exact expected value instead.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The click fails before the wait can complete

For normal UI controls, use a locator action so Playwright can apply actionability checks. If a locator cannot interact, investigate whether the element is obscured, not ready, or otherwise unavailable rather than replacing the action with a direct event just to make the test pass. Use coordinates only when that is the behavior under test.

Rank #4
Amazon Basics 3-Button USB Wired Mouse with Responsive Tracking, Plug & Play, Compatible with Windows and Mac, Black
  • Computer mouse for easily navigating a computer interface; click, scroll, and more
  • USB-A wired connection; if existing device only supports USB-C, an additional adapter will be required
  • High-definition (1000 dpi) optical tracking ensures responsive cursor control for precise tracking and easy text selection
  • 3 buttons offer effortless fingertip control
  • Plug-and-go ready for instant use

A synthetic click passes but real interaction does not

A direct dispatchEvent('click') does not establish that the element is visible or reachable by a user-like pointer. Switch to a locator or mouse action if the test should cover those conditions.

The wait appears to hang

waitForFunction documents a timeout default of 0, meaning no timeout. Provide an appropriate finite timeout or configure the applicable page/context defaults so a missing condition fails in bounded time.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance and test-suite considerations

Use the narrowest predicate that directly observes the required state; avoid polling for a broad condition unrelated to the interaction. Keep waits close to the action they validate so failures identify the relevant step. Coordinate interactions can be sensitive to viewport and layout, so use stable page setup and prefer locators when possible. No fixed timeout value fits every suite: select one based on the application’s expected behavior and the test environment, and treat timeout changes as a diagnostic choice rather than a substitute for a correct condition.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Or skip the browser setup

If the task is capturing a page rather than testing mouse behavior, ScreenshotNeo provides a website screenshot API and MCP server. It does not replace Playwright interaction tests; it is an option for generating screenshots or PDFs without setting up browser capture code yourself. One GET request can return PNG, JPEG, WebP, or PDF output.

Best Value
Sale
Acer Wireless Mouse for Laptop, 2.4GHz Computer Mouse 3 Adjustable 1600 DPI
  • 【Plug and Play for Home/Office/School】The wireless computer mouse features 2.4GHz connectivity, delivering a stable, interference-free connection up to 32ft. Designed for 𝐦𝐞𝐝𝐢𝐮𝐦 𝐭𝐨 𝐥𝐚𝐫𝐠𝐞 𝐬𝐢𝐳𝐞𝐝 𝐡𝐚𝐧𝐝𝐬, it ensures comfortable use all day. Simply plug in the USB-A receiver for instant pairing—no drivers needed. 📌📌 If the mouse isn’t suitable, place the USB receiver in the battery compartment and return both.
  • 【3 Levels Adjustable DPI】This travel USB mouse offers 3 adjustable DPI settings (800, 1200, 1600), allowing you to customize sensitivity for precise design work. Effortlessly switch to match your task and elevate your productivity. 📌 Please remove the film at the bottom of the mouse before use.
  • 【Effortless Browsing】Equipped with forward and backward buttons, this computer mice streamlines your workflow, making it easy to navigate through web pages and files with a simple click. 📌Side button does not work on Mac.
  • 【Visible Indicator Light】 The pc mouse features a visual indicator for DPI levels and low battery alerts. The red light flashes once for 800 DPI, twice for 1200 DPI, and three times for 1600 DPI. When the battery level is below 10%, the light flashes red until the mouse is completely out of power.
  • 【Click to Wake】With smart sleep mode, it saves power by standby after 10 inactive minutes, just 2-3 clicks to wake. This efficient design delivers 3x longer battery life than motion-wake mice. Engineered for durability, its buttons and scroll wheel are tested for 10 million clicks, ensuring long-term reliability and consistent performance.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo documentation for request options. Cookie/consent banners are accepted and removed before capture, as are supported newsletter popups and chat widgets. Bot checks/CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers indicate the page verdict and billing outcome. Its MCP server exposes screenshot and page-information tools for AI agents. The free plan includes 1,000 shots a month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo free: 1,000 screenshots a month, no card required.

Frequently Asked Questions

Does waitForFunction click or hover an element?

No. It waits for a truthy page-side condition; perform the mouse or locator action separately.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Should I use page.mouse for every click?

No. Use locator actions for ordinary element-targeted interactions; use page.mouse when coordinates or input sequencing matter.

Quick Recap

SaleBestseller No. 1
Logitech M185 Compact Ambidextrous 2.4 GHz Wireless Mouse - Swift Grey
Logitech M185 Compact Ambidextrous 2.4 GHz Wireless Mouse - Swift Grey
Product carbon footprint: 3.97 kg CO2e; Contoured shape: Gives you more comfort and control
$14.90
SaleBestseller No. 3
Bestseller No. 4
Amazon Basics 3-Button USB Wired Mouse with Responsive Tracking, Plug & Play, Compatible with Windows and Mac, Black
Amazon Basics 3-Button USB Wired Mouse with Responsive Tracking, Plug & Play, Compatible with Windows and Mac, Black
Computer mouse for easily navigating a computer interface; click, scroll, and more; 3 buttons offer effortless fingertip control
$9.70

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.