We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Simulating stylus input (pen pointer) for drawing and pointer events in web apps
In this practice lab, you will learn how to simulate a pen/stylus device in Selenium using the Pen pointer type.
A Pen in Selenium is a kind of pointer input (like mouse or touch) but with extra properties:
A Pen has 3 main button states:
0 — Touch Contact (default; equivalent to left click / pen tip touching)2 — Barrel Button (equivalent to right click)5 — Eraser Button (currently unsupported)When to use it
This is the higher-level, easier approach:
Tells Selenium to use a pen pointer device for the next pointer actions.
Then you can call: moveToElement(), clickAndHold(), moveByOffset(), release(), perform()
For advanced scenarios:
PointerInput of type PENSequence of pointer actions:createPointerMove(), createPointerDown(), createPointerMove(..., eventProperties), createPointerUp()PointerInput.eventProperties() to set:setTiltX(), setTiltY(), setTwist()Imagine a page called "Pen Drawing Pad" with a simple drawing area:
Draw with your pen here
Stroke info
No strokes yet
Draw on the canvas to simulate pen interactions! The stroke info shows the dimensions of your last stroke.
With a real stylus:
Your Selenium script will simulate this exact behavior using pen actions.
1234567891011121314WebElement pointerArea = driver.findElement(By.id("pointerArea"));
Actions actions = new Actions(driver);
actions
.setActivePointer(PointerInput.Kind.PEN, "default pen")
.moveToElement(pointerArea)
.clickAndHold()
.moveByOffset(50, 0)
.moveByOffset(0, 50)
.release()
.perform();
driver.quit();id="pointerArea".WebDriver instance."default pen" is an internal name for this virtual pen.Remember that Pen actions are Chromium-only—always run these tests on Chrome/Edge (or other Chromium-based drivers).
Start by testing with a mouse-based version of the test (same moves, but default pointer), then switch to Pen when your app uses pointer events and cares about the device type.
Keep drawing paths simple and deterministic (fixed offsets like 50,0 then 0,50) to avoid flaky tests caused by tiny movement differences.