Webdriver Actions接口请求Payload示例咨询(适配Firefox)
I get exactly what you're asking for—you want the raw JSON payloads that get sent to the /session/{sessionId}/actions endpoint, not the wrapper code from Selenium or other libraries. Let's break down practical, working examples tailored for Firefox, since Chrome's implementation is still catching up.
All these are POST requests with Content-Type: application/json to the endpoint mentioned above. Let's start with the basics:
1. Simple Left-Click on an Element
This payload moves the mouse to a target element (using its coordinates relative to the element) and performs a left-click.
{ "actions": [ { "id": "mouse1", "type": "pointer", "parameters": { "pointerType": "mouse" }, "actions": [ { "type": "pointerMove", "origin": "element", "element": "ELEMENT_ID_HERE", "x": 10, "y": 10 }, { "type": "pointerDown", "button": 0 }, { "type": "pointerUp", "button": 0 } ] } ] }
Key Notes:
- Replace
ELEMENT_ID_HEREwith the actual element ID you retrieved from a previousfindElementcall. button: 0corresponds to the left mouse button;1is middle,2is right.- The
x/yvalues are offsets from the top-left corner of the element (adjust as needed).
2. Drag-and-Drop an Element
This sequence moves the mouse to the source element, presses the left button, moves to the target element, then releases the button.
{ "actions": [ { "id": "mouse1", "type": "pointer", "parameters": { "pointerType": "mouse" }, "actions": [ { "type": "pointerMove", "origin": "element", "element": "SOURCE_ELEMENT_ID", "x": 5, "y": 5 }, { "type": "pointerDown", "button": 0 }, { "type": "pointerMove", "origin": "element", "element": "TARGET_ELEMENT_ID", "x": 10, "y": 10 }, { "type": "pointerUp", "button": 0 } ] } ] }
Key Notes:
- If you want to drag by pixel offsets instead of targeting an element, set
origin: "viewport"and use absolutex/yvalues. - Keep the
pointerDownandpointerUppaired—don't forget to release the button!
3. Keyboard: Insert Text (Simplified)
Firefox supports the insertText action, which lets you input a full string in one step instead of simulating each keypress.
{ "actions": [ { "id": "keyboard1", "type": "key", "actions": [ { "type": "insertText", "value": "Hello, Firefox Actions!" } ] } ] }
4. Keyboard: Simulate CTRL+C Shortcut
This payload presses and releases the Ctrl key along with the C key to copy text.
{ "actions": [ { "id": "keyboard1", "type": "key", "actions": [ { "type": "keyDown", "value": "\uE009" // Unicode for Left Control }, { "type": "keyDown", "value": "c" }, { "type": "keyUp", "value": "c" }, { "type": "keyUp", "value": "\uE009" } ] } ] }
Key Notes:
- Special keys use Unicode values from the WebDriver spec, e.g.,
\uE00Ais Right Control,\uE00Bis Left Shift. - Always pair
keyDownwithkeyUpto avoid leaving keys stuck in a pressed state.
5. Combined Mouse + Keyboard Action (Shift-Click)
This example holds the Shift key while clicking an element.
{ "actions": [ { "id": "keyboard1", "type": "key", "actions": [ { "type": "keyDown", "value": "\uE00B" // Left Shift } ] }, { "id": "mouse1", "type": "pointer", "parameters": { "pointerType": "mouse" }, "actions": [ { "type": "pointerMove", "origin": "element", "element": "TARGET_ELEMENT_ID", "x": 0, "y": 0 }, { "type": "pointerDown", "button": 0 }, { "type": "pointerUp", "button": 0 } ] }, { "id": "keyboard1", "type": "key", "actions": [ { "type": "keyUp", "value": "\uE00B" } ] } ] }
Key Notes:
- The
actionsarray can include multiple input devices—WebDriver executes them in sequence, so the Shift key is pressed before the click, then released after.
A quick reminder: Make sure your Firefox version is up-to-date (most modern versions support these payloads fully). If you run into issues, double-check that the element IDs are valid and that you're sending the payload to the correct session endpoint.
内容的提问来源于stack exchange,提问作者Merc

