Hover
Elements that change their behavior or appearance when hovered.
Hovering and hover-only content
hover() moves the mouse over an element
hover() scrolls the element into view and moves the mouse to its centre. To stop hovering, hover something else, such as the page heading.
await page.getByText('Hover over me').hover();
await expect(page.getByText('Hover state: active')).toBeVisible();
await page.getByRole('heading', { name: 'Hover', exact: true }).hover();
await expect(page.getByText('Hover state: inactive')).toBeVisible();Menus that only appear on hover
Hidden menu items exist in the page but cannot be clicked. Hover the trigger first, then click while the menu is open. The mouse moves straight from the trigger to the option, so the menu stays open.
const option = page.getByRole('button', { name: 'Option 2' });
await expect(option).toBeHidden();
await page.getByRole('button', { name: 'Move mouse here' }).hover();
await option.click();Tooltips
Tooltips have the tooltip role and are linked to their trigger with aria-describedby. That gives two ways to assert one:
const info = page.getByRole('button', { name: 'Shipping info' });
await info.hover();
await expect(page.getByRole('tooltip')).toHaveText('Free shipping on orders over 200 PLN');
await expect(info).toHaveAccessibleDescription('Free shipping on orders over 200 PLN');Basic hover
Hover over me
Hover state: inactive
Hover menu
Menu choice: none
Tooltip
Next section · LocatorsAccessibility Tree →Find elements by the role and name the browser gives them, not by how they look.
Related exercises
- Mouse & KeyboardIntermediateMouse ActionsUse the low-level mouse: modifier clicks, positions, drawing, the wheel and a slider.
- Basic ElementsBeginnerButtonsUse the three kinds of click and verify the result of each.
- Dynamic ElementsIntermediateActionabilityUnderstand what Playwright waits for before it clicks, and what force: true hides.
Read the guide: Playwright practice for beginners: a four-week plan