Actions API A low-level interface for providing virtualized device input actions to the web browser.
In addition to the high-level element interactions ,
the Actions API provides granular control over
exactly what designated input devices can do. Selenium provides an interface for 3 kinds of input sources:
a key input for keyboard devices, a pointer input for a mouse, pen or touch devices,
and wheel inputs for scroll wheel devices (introduced in Selenium 4.2).
Selenium allows you to construct individual action commands assigned to specific
inputs and chain them together and call the associated perform method to execute them all at once.
Action Builder In the move from the legacy JSON Wire Protocol to the new W3C WebDriver Protocol,
the low level building blocks of actions became especially detailed. It is extremely
powerful, but each input device has a number of ways to use it and if you need to
manage more than one device, you are responsible for ensuring proper synchronization between them.
Thankfully, you likely do not need to learn how to use the low level commands directly, since
almost everything you might want to do has been given a convenience method that combines the
lower level commands for you. These are all documented in
keyboard , mouse , pen , and wheel pages.
Pause Pointer movements and Wheel scrolling allow the user to set a duration for the action, but sometimes you just need
to wait a beat between actions for things to work correctly.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. moveToElement ( clickable )
. pause ( Duration . ofSeconds ( 1 ))
. clickAndHold ()
. pause ( Duration . ofSeconds ( 1 ))
. sendKeys ( "abc" )
. perform ();
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. move_to_element ( clickable ) \
. pause ( 1 ) \
. click_and_hold () \
. pause ( 1 ) \
. send_keys ( "abc" ) \
. perform ()
Selenium v4.2
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. MoveToElement ( clickable )
. Pause ( TimeSpan . FromSeconds ( 1 ))
. ClickAndHold ()
. Pause ( TimeSpan . FromSeconds ( 1 ))
. SendKeys ( "abc" )
. Perform ();
Selenium v4.2
clickable = driver . find_element ( id : 'clickable' )
driver . action
. move_to ( clickable )
. pause ( duration : 1 )
. click_and_hold
. pause ( duration : 1 )
. send_keys ( 'abc' )
. perform
const clickable = await driver . findElement ( By . id ( 'clickable' ))
await driver . actions ()
. move ({ origin : clickable })
. pause ( 1000 )
. press ()
. pause ( 1000 )
. sendKeys ( 'abc' )
. perform ()
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. moveToElement ( clickable )
. pause ( Duration . ofSeconds ( 1 ))
. clickAndHold ()
. pause ( Duration . ofSeconds ( 1 ))
. sendKeys ( "abc" )
. perform ()
Release All Actions An important thing to note is that the driver remembers the state of all the input
items throughout a session. Even if you create a new instance of an actions class, the depressed keys and
the location of the pointer will be in whatever state a previously performed action left them.
There is a special method to release all currently depressed keys and pointer buttons.
This method is implemented differently in each of the languages because
it does not get executed with the perform method.
Java
Python
CSharp
Ruby
JavaScript
Kotlin (( RemoteWebDriver ) driver ). resetInputState ();
ActionBuilder ( driver ) . clear_actions ()
(( WebDriver ) driver ). ResetInputState ();
driver . action . release_actions
await driver . actions (). clear ()
( driver as RemoteWebDriver ). resetInputState ()
1 - Keyboard actions A representation of any key input device for interacting with a web page.
There are only 2 actions that can be accomplished with a keyboard:
pressing down on a key, and releasing a pressed key.
In addition to supporting ASCII characters, each keyboard key has
a representation that can be pressed or released in designated sequences.
Keys In addition to the keys represented by regular unicode,
unicode values have been assigned to other keyboard keys for use with Selenium.
Each language has its own way to reference these keys; the full list can be found
here .
Java
Python
CSharp
Ruby
JavaScript
Kotlin Key down
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ();
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "abc" ) \
. perform ()
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. Perform ();
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. perform
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. perform ()
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. perform ()
Key up
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ();
ActionChains ( driver ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( "a" ) \
. key_up ( Keys . SHIFT ) \
. send_keys ( "b" ) \
. perform ()
new Actions ( driver )
. KeyDown ( Keys . Shift )
. SendKeys ( "a" )
. KeyUp ( Keys . Shift )
. SendKeys ( "b" )
. Perform ();
driver . action
. key_down ( :shift )
. send_keys ( 'a' )
. key_up ( :shift )
. send_keys ( 'b' )
. perform
await driver . actions ()
. keyDown ( Key . SHIFT )
. sendKeys ( 'a' )
. keyUp ( Key . SHIFT )
. sendKeys ( 'b' )
. perform ()
Actions ( driver )
. keyDown ( Keys . SHIFT )
. sendKeys ( "a" )
. keyUp ( Keys . SHIFT )
. sendKeys ( "b" )
. perform ()
Send keys This is a convenience method in the Actions API that combines keyDown and keyUp commands in one action.
Executing this command differs slightly from using the element method, but
primarily this gets used when needing to type multiple characters in the middle of other actions.
Active Element
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. sendKeys ( "abc" )
. perform ();
ActionChains ( driver ) \
. send_keys ( "abc" ) \
. perform ()
new Actions ( driver )
. SendKeys ( "abc" )
driver . action
. send_keys ( 'abc' )
. perform
const textField = driver . findElement ( By . id ( 'textInput' ))
await textField . click ()
Actions ( driver )
. sendKeys ( "abc" )
. perform ()
Designated Element
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ();
text_input = driver . find_element ( By . ID , "textInput" )
ActionChains ( driver ) \
. send_keys_to_element ( text_input , "abc" ) \
. perform ()
driver . FindElement ( By . TagName ( "body" )). Click ();
IWebElement textField = driver . FindElement ( By . Id ( "textInput" ));
new Actions ( driver )
text_field = driver . find_element ( id : 'textInput' )
driver . action
. send_keys ( text_field , 'Selenium!' )
. perform
Selenium v4.5.0
const textField = await driver . findElement ( By . id ( 'textInput' ))
await driver . actions ()
. sendKeys ( textField , 'abc' )
. perform ()
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. perform ()
Copy and Paste Here’s an example of using all of the above methods to conduct a copy / paste action.
Note that the key to use for this operation will be different depending on if it is a Mac OS or not.
This code will end up with the text: SeleniumSelenium!
Java
Python
CSharp
Ruby
JavaScript
Kotlin Keys cmdCtrl = Platform . getCurrent (). is ( Platform . MAC ) ? Keys . COMMAND : Keys . CONTROL ;
WebElement textField = driver . findElement ( By . id ( "textInput" ));
new Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ();
Assertions . assertEquals ( "SeleniumSelenium!" , textField . getAttribute ( "value" ));
cmd_ctrl = Keys . COMMAND if sys . platform == 'darwin' else Keys . CONTROL
ActionChains ( driver ) \
. send_keys ( "Selenium!" ) \
. send_keys ( Keys . ARROW_LEFT ) \
. key_down ( Keys . SHIFT ) \
. send_keys ( Keys . ARROW_UP ) \
. key_up ( Keys . SHIFT ) \
. key_down ( cmd_ctrl ) \
. send_keys ( "xvv" ) \
. key_up ( cmd_ctrl ) \
. perform ()
var capabilities = (( WebDriver ) driver ). Capabilities ;
String platformName = ( string ) capabilities . GetCapability ( "platformName" );
String cmdCtrl = platformName . Contains ( "mac" ) ? Keys . Command : Keys . Control ;
new Actions ( driver )
. SendKeys ( "Selenium!" )
. SendKeys ( Keys . ArrowLeft )
. KeyDown ( Keys . Shift )
. SendKeys ( Keys . ArrowUp )
cmd_ctrl = driver . capabilities . platform_name . include? ( 'mac' ) ? :command : :control
driver . action
. send_keys ( 'Selenium!' )
. send_keys ( :arrow_left )
. key_down ( :shift )
. send_keys ( :arrow_up )
. key_up ( :shift )
. key_down ( cmd_ctrl )
. send_keys ( 'xvv' )
. key_up ( cmd_ctrl )
. perform
const cmdCtrl = platform . includes ( 'darwin' ) ? Key . COMMAND : Key . CONTROL
await driver . actions ()
. click ( textField )
. sendKeys ( 'Selenium!' )
. sendKeys ( Key . ARROW_LEFT )
. keyDown ( Key . SHIFT )
. sendKeys ( Key . ARROW_UP )
. keyUp ( Key . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( 'xvv' )
. keyUp ( cmdCtrl )
. perform ()
val cmdCtrl = if ( platformName == Platform . MAC ) Keys . COMMAND else Keys . CONTROL
val textField = driver . findElement ( By . id ( "textInput" ))
Actions ( driver )
. sendKeys ( textField , "Selenium!" )
. sendKeys ( Keys . ARROW_LEFT )
. keyDown ( Keys . SHIFT )
. sendKeys ( Keys . ARROW_UP )
. keyUp ( Keys . SHIFT )
. keyDown ( cmdCtrl )
. sendKeys ( "xvv" )
. keyUp ( cmdCtrl )
. perform ()
2 - Mouse actions A representation of any pointer device for interacting with a web page.
There are only 3 actions that can be accomplished with a mouse:
pressing down on a button, releasing a pressed button, and moving the mouse.
Selenium provides convenience methods that combine these actions in the most common ways.
Click and hold This method combines moving the mouse to the center of an element with pressing the left mouse button.
This is useful for focusing a specific element:
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. clickAndHold ( clickable )
. perform ();
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. click_and_hold ( clickable ) \
. perform ()
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ClickAndHold ( clickable )
. Perform ();
clickable = driver . find_element ( id : 'clickable' )
driver . action
. click_and_hold ( clickable )
. perform
let clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : clickable }). press (). perform ();
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. clickAndHold ( clickable )
. perform ()
Click and release This method combines moving to the center of an element with pressing and releasing the left mouse button.
This is otherwise known as “clicking”:
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "click" ));
new Actions ( driver )
. click ( clickable )
. perform ();
clickable = driver . find_element ( By . ID , "click" )
ActionChains ( driver ) \
. click ( clickable ) \
. perform ()
IWebElement clickable = driver . FindElement ( By . Id ( "click" ));
new Actions ( driver )
. Click ( clickable )
. Perform ();
clickable = driver . find_element ( id : 'click' )
driver . action
. click ( clickable )
. perform
let click = driver . findElement ( By . id ( "click" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : click }). click (). perform ();
val clickable = driver . findElement ( By . id ( "click" ))
Actions ( driver )
. click ( clickable )
. perform ()
There are a total of 5 defined buttons for a Mouse:
0 — Left Button (the default) 1 — Middle Button (currently unsupported) 2 — Right Button 3 — X1 (Back) Button 4 — X2 (Forward) Button Context Click This method combines moving to the center of an element with pressing and releasing the right mouse button (button 2).
This is otherwise known as “right-clicking”:
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. contextClick ( clickable )
. perform ();
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. context_click ( clickable ) \
. perform ()
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. ContextClick ( clickable )
. Perform ();
clickable = driver . find_element ( id : 'clickable' )
driver . action
. context_click ( clickable )
. perform
const clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . contextClick ( clickable ). perform ();
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. contextClick ( clickable )
. perform ()
Back Click There is no convenience method for this, it is just pressing and releasing mouse button 3
Java
Python
CSharp
Ruby
JavaScript
Kotlin PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Selenium v4.2
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . BACK )
action . pointer_action . pointer_up ( MouseButton . BACK )
action . perform ()
Selenium v4.2
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Selenium v4.2
driver . action
. pointer_down ( :back )
. pointer_up ( :back )
. perform
Selenium v4.5.0
const actions = driver . actions ({ async : true });
await actions . press ( Button . BACK ). release ( Button . BACK ). perform ()
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Forward Click There is no convenience method for this, it is just pressing and releasing mouse button 4
Java
Python
CSharp
Ruby
JavaScript
Kotlin PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
Selenium v4.2
action = ActionBuilder ( driver )
action . pointer_action . pointer_down ( MouseButton . FORWARD )
action . pointer_action . pointer_up ( MouseButton . FORWARD )
action . perform ()
Selenium v4.2
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward ));
actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Selenium v4.2
driver . action
. pointer_down ( :forward )
. pointer_up ( :forward )
. perform
Selenium v4.5.0
const actions = driver . actions ({ async : true });
await actions . press ( Button . FORWARD ). release ( Button . FORWARD ). perform ()
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ()))
. addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ()))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Double click This method combines moving to the center of an element with pressing and releasing the left mouse button twice.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement clickable = driver . findElement ( By . id ( "clickable" ));
new Actions ( driver )
. doubleClick ( clickable )
. perform ();
clickable = driver . find_element ( By . ID , "clickable" )
ActionChains ( driver ) \
. double_click ( clickable ) \
. perform ()
IWebElement clickable = driver . FindElement ( By . Id ( "clickable" ));
new Actions ( driver )
. DoubleClick ( clickable )
. Perform ();
clickable = driver . find_element ( id : 'clickable' )
driver . action
. double_click ( clickable )
. perform
const clickable = driver . findElement ( By . id ( "clickable" ));
const actions = driver . actions ({ async : true });
await actions . doubleClick ( clickable ). perform ();
val clickable = driver . findElement ( By . id ( "clickable" ))
Actions ( driver )
. doubleClick ( clickable )
. perform ()
Move to element This method moves the mouse to the in-view center point of the element.
This is otherwise known as “hovering.”
Note that the element must be in the viewport or else the command will error.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement hoverable = driver . findElement ( By . id ( "hover" ));
new Actions ( driver )
. moveToElement ( hoverable )
. perform ();
hoverable = driver . find_element ( By . ID , "hover" )
ActionChains ( driver ) \
. move_to_element ( hoverable ) \
. perform ()
IWebElement hoverable = driver . FindElement ( By . Id ( "hover" ));
new Actions ( driver )
. MoveToElement ( hoverable )
. Perform ();
hoverable = driver . find_element ( id : 'hover' )
driver . action
. move_to ( hoverable )
. perform
const hoverable = driver . findElement ( By . id ( "hover" ));
const actions = driver . actions ({ async : true });
await actions . move ({ origin : hoverable }). perform ();
val hoverable = driver . findElement ( By . id ( "hover" ))
Actions ( driver )
. moveToElement ( hoverable )
. perform ()
Move by offset These methods first move the mouse to the designated origin and then
by the number of pixels in the provided offset.
Note that the position of the mouse must be in the viewport or else the command will error.
Offset from Element This method moves the mouse to the in-view center point of the element,
then moves by the provided offset.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement tracker = driver . findElement ( By . id ( "mouse-tracker" ));
new Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ();
mouse_tracker = driver . find_element ( By . ID , "mouse-tracker" )
ActionChains ( driver ) \
. move_to_element_with_offset ( mouse_tracker , 8 , 0 ) \
. perform ()
IWebElement tracker = driver . FindElement ( By . Id ( "mouse-tracker" ));
new Actions ( driver )
. MoveToElement ( tracker , 8 , 0 )
. Perform ();
mouse_tracker = driver . find_element ( id : 'mouse-tracker' )
driver . action
. move_to ( mouse_tracker , 8 , 11 )
. perform
val tracker = driver . findElement ( By . id ( "mouse-tracker" ))
Actions ( driver )
. moveToElement ( tracker , 8 , 0 )
. perform ()
Offset from Viewport This method moves the mouse from the upper left corner of the current viewport by the provided
offset.
Java
Python
CSharp
Ruby
JavaScript
Kotlin PointerInput mouse = new PointerInput ( PointerInput . Kind . MOUSE , "default mouse" );
Sequence actions = new Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actions ));
action = ActionBuilder ( driver )
action . pointer_action . move_to_location ( 8 , 0 )
action . perform ()
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice mouse = new PointerInputDevice ( PointerKind . Mouse , "default mouse" );
actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport ,
8 , 0 , TimeSpan . Zero ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
driver . action
. move_to_location ( 8 , 12 )
. perform
const actions = driver . actions ({ async : true });
await actions . move ({ x : 8 , y : 0 }). perform ();
val mouse = PointerInput ( PointerInput . Kind . MOUSE , "default mouse" )
val actions = Sequence ( mouse , 0 )
. addAction ( mouse . createPointerMove ( Duration . ZERO , PointerInput . Origin . viewport (), 8 , 12 ))
( driver as RemoteWebDriver ). perform ( Collections . singletonList ( actions ))
Offset from Current Pointer Location This method moves the mouse from its current position by the offset provided by the user.
If the mouse has not previously been moved, the position will be in the upper left
corner of the viewport.
Note that the pointer position does not change when the page is scrolled.
Note that the first argument X specifies to move right when positive, while the second argument
Y specifies to move down when positive. So moveByOffset(30, -10)
moves right 30 and up 10 from
the current mouse position.
Java
Python
CSharp
Ruby
JavaScript
Kotlin new Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ();
ActionChains ( driver ) \
. move_by_offset ( 13 , 15 ) \
. perform ()
new Actions ( driver )
. MoveByOffset ( 13 , 15 )
. Perform ();
driver . action
. move_by ( 13 , 15 )
. perform
await actions . move ({ x : 13 , y : 15 , origin : Origin . POINTER }). perform ()
Actions ( driver )
. moveByOffset ( 13 , 15 )
. perform ()
Drag and Drop on Element This method firstly performs a click-and-hold on the source element,
moves to the location of the target element and then releases the mouse.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement draggable = driver . findElement ( By . id ( "draggable" ));
WebElement droppable = driver . findElement ( By . id ( "droppable" ));
new Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ();
draggable = driver . find_element ( By . ID , "draggable" )
droppable = driver . find_element ( By . ID , "droppable" )
ActionChains ( driver ) \
. drag_and_drop ( draggable , droppable ) \
. perform ()
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
IWebElement droppable = driver . FindElement ( By . Id ( "droppable" ));
new Actions ( driver )
. DragAndDrop ( draggable , droppable )
. Perform ();
draggable = driver . find_element ( id : 'draggable' )
droppable = driver . find_element ( id : 'droppable' )
driver . action
. drag_and_drop ( draggable , droppable )
. perform
const draggable = driver . findElement ( By . id ( "draggable" ));
const droppable = await driver . findElement ( By . id ( "droppable" ));
const actions = driver . actions ({ async : true });
await actions . dragAndDrop ( draggable , droppable ). perform ();
val draggable = driver . findElement ( By . id ( "draggable" ))
val droppable = driver . findElement ( By . id ( "droppable" ))
Actions ( driver )
. dragAndDrop ( draggable , droppable )
. perform ()
Drag and Drop by Offset This method firstly performs a click-and-hold on the source element, moves to the given offset and then releases the mouse.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement draggable = driver . findElement ( By . id ( "draggable" ));
Rectangle start = draggable . getRect ();
Rectangle finish = driver . findElement ( By . id ( "droppable" )). getRect ();
new Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ();
draggable = driver . find_element ( By . ID , "draggable" )
start = draggable . location
finish = driver . find_element ( By . ID , "droppable" ) . location
ActionChains ( driver ) \
. drag_and_drop_by_offset ( draggable , finish [ 'x' ] - start [ 'x' ], finish [ 'y' ] - start [ 'y' ]) \
. perform ()
IWebElement draggable = driver . FindElement ( By . Id ( "draggable" ));
Point start = draggable . Location ;
Point finish = driver . FindElement ( By . Id ( "droppable" )). Location ;
new Actions ( driver )
. DragAndDropToOffset ( draggable , finish . X - start . X , finish . Y - start . Y )
. Perform ();
draggable = driver . find_element ( id : 'draggable' )
start = draggable . rect
finish = driver . find_element ( id : 'droppable' ) . rect
driver . action
. drag_and_drop_by ( draggable , finish . x - start . x , finish . y - start . y )
. perform
const draggable = driver . findElement ( By . id ( "draggable" ));
let start = await draggable . getRect ();
let finish = await driver . findElement ( By . id ( "droppable" )). getRect ();
const actions = driver . actions ({ async : true });
await actions . dragAndDrop ( draggable , { x : finish . x - start . x , y : finish . y - start . y }). perform ();
val draggable = driver . findElement ( By . id ( "draggable" ))
val start = draggable . getRect ()
val finish = driver . findElement ( By . id ( "droppable" )). getRect ()
Actions ( driver )
. dragAndDropBy ( draggable , finish . getX () - start . getX (), finish . getY () - start . getY ())
. perform ()
3 - Pen actions A representation of a pen stylus kind of pointer input for interacting with a web page.
Chromium Only
A Pen is a type of pointer input that has most of the same behavior as a mouse, but can
also have event properties unique to a stylus. Additionally, while a mouse
has 5 buttons, a pen has 3 equivalent button states:
0 — Touch Contact (the default; equivalent to a left click) 2 — Barrel Button (equivalent to a right click) 5 — Eraser Button (currently unsupported by drivers) Using a Pen
Java
Python
CSharp
Ruby
JavaScript
Kotlin Selenium v4.2
WebElement pointerArea = driver . findElement ( By . id ( "pointerArea" ));
new Actions ( driver )
. setActivePointer ( PointerInput . Kind . PEN , "default pen" )
. moveToElement ( pointerArea )
. clickAndHold ()
. moveByOffset ( 2 , 2 )
. release ()
. perform ();
Selenium v4.2
pointer_area = driver . find_element ( By . ID , "pointerArea" )
pen_input = PointerInput ( POINTER_PEN , "default pen" )
action = ActionBuilder ( driver , mouse = pen_input )
action . pointer_action \
. move_to ( pointer_area ) \
. pointer_down () \
. move_by ( 2 , 2 ) \
. pointer_up ()
action . perform ()
IWebElement pointerArea = driver . FindElement ( By . Id ( "pointerArea" ));
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice pen = new PointerInputDevice ( PointerKind . Pen , "default pen" );
actionBuilder . AddAction ( pen . CreatePointerMove ( pointerArea , 0 , 0 , TimeSpan . FromMilliseconds ( 800 )));
actionBuilder . AddAction ( pen . CreatePointerDown ( MouseButton . Left ));
actionBuilder . AddAction ( pen . CreatePointerMove ( CoordinateOrigin . Pointer ,
2 , 2 , TimeSpan . Zero ));
actionBuilder . AddAction ( pen . CreatePointerUp ( MouseButton . Left ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
Selenium v4.2
pointer_area = driver . find_element ( id : 'pointerArea' )
driver . action ( devices : :pen )
. move_to ( pointer_area )
. pointer_down
. move_by ( 2 , 2 )
. pointer_up
. perform
val pointerArea = driver . findElement ( By . id ( "pointerArea" ))
Actions ( driver )
. setActivePointer ( PointerInput . Kind . PEN , "default pen" )
. moveToElement ( pointerArea )
. clickAndHold ()
. moveByOffset ( 2 , 2 )
. release ()
. perform ()
Adding Pointer Event Attributes Selenium v4.2
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement pointerArea = driver . findElement ( By . id ( "pointerArea" ));
PointerInput pen = new PointerInput ( PointerInput . Kind . PEN , "default pen" );
PointerInput . PointerEventProperties eventProperties = PointerInput . eventProperties ()
. setTiltX (- 72 )
. setTiltY ( 9 )
. setTwist ( 86 );
PointerInput . Origin origin = PointerInput . Origin . fromElement ( pointerArea );
Sequence actionListPen = new Sequence ( pen , 0 )
. addAction ( pen . createPointerMove ( Duration . ZERO , origin , 0 , 0 ))
. addAction ( pen . createPointerDown ( 0 ))
. addAction ( pen . createPointerMove ( Duration . ZERO , origin , 2 , 2 , eventProperties ))
. addAction ( pen . createPointerUp ( 0 ));
(( RemoteWebDriver ) driver ). perform ( Collections . singletonList ( actionListPen ));
pointer_area = driver . find_element ( By . ID , "pointerArea" )
pen_input = PointerInput ( POINTER_PEN , "default pen" )
action = ActionBuilder ( driver , mouse = pen_input )
action . pointer_action \
. move_to ( pointer_area ) \
. pointer_down () \
. move_by ( 2 , 2 , tilt_x =- 72 , tilt_y = 9 , twist = 86 ) \
. pointer_up ( 0 )
action . perform ()
IWebElement pointerArea = driver . FindElement ( By . Id ( "pointerArea" ));
ActionBuilder actionBuilder = new ActionBuilder ();
PointerInputDevice pen = new PointerInputDevice ( PointerKind . Pen , "default pen" );
PointerInputDevice . PointerEventProperties properties = new PointerInputDevice . PointerEventProperties () {
TiltX = - 72 ,
TiltY = 9 ,
Twist = 86 ,
};
actionBuilder . AddAction ( pen . CreatePointerMove ( pointerArea , 0 , 0 , TimeSpan . FromMilliseconds ( 800 )));
actionBuilder . AddAction ( pen . CreatePointerDown ( MouseButton . Left ));
actionBuilder . AddAction ( pen . CreatePointerMove ( CoordinateOrigin . Pointer ,
2 , 2 , TimeSpan . Zero , properties ));
actionBuilder . AddAction ( pen . CreatePointerUp ( MouseButton . Left ));
(( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());
pointer_area = driver . find_element ( id : 'pointerArea' )
driver . action ( devices : :pen )
. move_to ( pointer_area )
. pointer_down
. move_by ( 2 , 2 , tilt_x : - 72 , tilt_y : 9 , twist : 86 )
. pointer_up
. perform
val pointerArea = driver . findElement ( By . id ( "pointerArea" ))
val pen = PointerInput ( PointerInput . Kind . PEN , "default pen" )
val eventProperties = PointerInput . eventProperties ()
. setTiltX (- 72 )
. setTiltY ( 9 )
. setTwist ( 86 )
val origin = PointerInput . Origin . fromElement ( pointerArea )
val actionListPen = Sequence ( pen , 0 )
. addAction ( pen . createPointerMove ( Duration . ZERO , origin , 0 , 0 ))
. addAction ( pen . createPointerDown ( 0 ))
. addAction ( pen . createPointerMove ( Duration . ZERO , origin , 2 , 2 , eventProperties ))
. addAction ( pen . createPointerUp ( 0 ))
( driver as RemoteWebDriver ). perform ( listOf ( actionListPen ))
4 - Scroll wheel actions A representation of a scroll wheel input device for interacting with a web page.
Selenium v4.2
Chromium Only
There are 5 scenarios for scrolling on a page.
This is the most common scenario. Unlike traditional click and send keys methods,
the actions class does not automatically scroll the target element into view,
so this method will need to be used if elements are not already inside the viewport.
This method takes a web element as the sole argument.
Regardless of whether the element is above or below the current viewscreen,
the viewport will be scrolled so the bottom of the element is at the bottom of the screen.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement iframe = driver . findElement ( By . tagName ( "iframe" ));
new Actions ( driver )
. scrollToElement ( iframe )
. perform ();
iframe = driver . find_element ( By . TAG_NAME , "iframe" )
ActionChains ( driver ) \
. scroll_to_element ( iframe ) \
. perform ()
IWebElement iframe = driver . FindElement ( By . TagName ( "iframe" ));
new Actions ( driver )
. ScrollToElement ( iframe )
. Perform ();
iframe = driver . find_element ( tag_name : 'iframe' )
driver . action
. scroll_to ( iframe )
. perform
const iframe = await driver . findElement ( By . css ( "iframe" ))
await driver . actions ()
. scroll ( 0 , 0 , 0 , 0 , iframe )
. perform ()
val iframe = driver . findElement ( By . tagName ( "iframe" ))
Actions ( driver )
. scrollToElement ( iframe )
. perform ()
This is the second most common scenario for scrolling. Pass in an delta x and a delta y value for how much to scroll
in the right and down directions. Negative values represent left and up, respectively.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement footer = driver . findElement ( By . tagName ( "footer" ));
int deltaY = footer . getRect (). y ;
new Actions ( driver )
. scrollByAmount ( 0 , deltaY )
. perform ();
footer = driver . find_element ( By . TAG_NAME , "footer" )
delta_y = footer . rect [ 'y' ]
ActionChains ( driver ) \
. scroll_by_amount ( 0 , delta_y ) \
. perform ()
IWebElement footer = driver . FindElement ( By . TagName ( "footer" ));
int deltaY = footer . Location . Y ;
new Actions ( driver )
. ScrollByAmount ( 0 , deltaY )
. Perform ();
footer = driver . find_element ( tag_name : 'footer' )
delta_y = footer . rect . y
driver . action
. scroll_by ( 0 , delta_y )
. perform
const footer = await driver . findElement ( By . css ( "footer" ))
const deltaY = ( await footer . getRect ()). y
await driver . actions ()
. scroll ( 0 , 0 , 0 , deltaY )
. perform ()
val footer = driver . findElement ( By . tagName ( "footer" ))
val deltaY = footer . getRect (). y
Actions ( driver )
. scrollByAmount ( 0 , deltaY )
. perform ()
This scenario is effectively a combination of the above two methods.
To execute this use the “Scroll From” method, which takes 3 arguments.
The first represents the origination point, which we designate as the element,
and the second two are the delta x and delta y values.
If the element is out of the viewport,
it will be scrolled to the bottom of the screen, then the page will be scrolled by the provided
delta x and delta y values.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement iframe = driver . findElement ( By . tagName ( "iframe" ));
WheelInput . ScrollOrigin scrollOrigin = WheelInput . ScrollOrigin . fromElement ( iframe );
new Actions ( driver )
. scrollFromOrigin ( scrollOrigin , 0 , 200 )
. perform ();
iframe = driver . find_element ( By . TAG_NAME , "iframe" )
scroll_origin = ScrollOrigin . from_element ( iframe )
ActionChains ( driver ) \
. scroll_from_origin ( scroll_origin , 0 , 200 ) \
. perform ()
IWebElement iframe = driver . FindElement ( By . TagName ( "iframe" ));
WheelInputDevice . ScrollOrigin scrollOrigin = new WheelInputDevice . ScrollOrigin
{
Element = iframe
};
new Actions ( driver )
. ScrollFromOrigin ( scrollOrigin , 0 , 200 )
. Perform ();
iframe = driver . find_element ( tag_name : 'iframe' )
scroll_origin = Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( iframe )
driver . action
. scroll_from ( scroll_origin , 0 , 200 )
. perform
const iframe = await driver . findElement ( By . css ( "iframe" ))
await driver . actions ()
. scroll ( 0 , 0 , 0 , 200 , iframe )
. perform ()
val iframe = driver . findElement ( By . tagName ( "iframe" ))
val scrollOrigin = WheelInput . ScrollOrigin . fromElement ( iframe )
Actions ( driver )
. scrollFromOrigin ( scrollOrigin , 0 , 200 )
. perform ()
This scenario is used when you need to scroll only a portion of the screen, and it is outside the viewport.
Or is inside the viewport and the portion of the screen that must be scrolled
is a known offset away from a specific element.
This uses the “Scroll From” method again, and in addition to specifying the element,
an offset is specified to indicate the origin point of the scroll. The offset is
calculated from the center of the provided element.
If the element is out of the viewport,
it first will be scrolled to the bottom of the screen, then the origin of the scroll will be determined
by adding the offset to the coordinates of the center of the element, and finally
the page will be scrolled by the provided delta x and delta y values.
Note that if the offset from the center of the element falls outside of the viewport,
it will result in an exception.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WebElement footer = driver . findElement ( By . tagName ( "footer" ));
WheelInput . ScrollOrigin scrollOrigin = WheelInput . ScrollOrigin . fromElement ( footer , 0 , - 50 );
new Actions ( driver )
. scrollFromOrigin ( scrollOrigin , 0 , 200 )
. perform ();
footer = driver . find_element ( By . TAG_NAME , "footer" )
scroll_origin = ScrollOrigin . from_element ( footer , 0 , - 50 )
ActionChains ( driver ) \
. scroll_from_origin ( scroll_origin , 0 , 200 ) \
. perform ()
IWebElement footer = driver . FindElement ( By . TagName ( "footer" ));
var scrollOrigin = new WheelInputDevice . ScrollOrigin
{
Element = footer ,
XOffset = 0 ,
YOffset = - 50
};
new Actions ( driver )
. ScrollFromOrigin ( scrollOrigin , 0 , 200 )
. Perform ();
footer = driver . find_element ( tag_name : 'footer' )
scroll_origin = Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( footer , 0 , - 50 )
driver . action
. scroll_from ( scroll_origin , 0 , 200 )
. perform
const footer = await driver . findElement ( By . css ( "footer" ))
await driver . actions ()
. scroll ( 0 , - 50 , 0 , 200 , footer )
. perform ()
val footer = driver . findElement ( By . tagName ( "footer" ))
val scrollOrigin = WheelInput . ScrollOrigin . fromElement ( footer , 0 , - 50 )
Actions ( driver )
. scrollFromOrigin ( scrollOrigin , 0 , 200 )
. perform ()
The final scenario is used when you need to scroll only a portion of the screen,
and it is already inside the viewport.
This uses the “Scroll From” method again, but the viewport is designated instead
of an element. An offset is specified from the upper left corner of the
current viewport. After the origin point is determined,
the page will be scrolled by the provided delta x and delta y values.
Note that if the offset from the upper left corner of the viewport falls outside of the screen,
it will result in an exception.
Java
Python
CSharp
Ruby
JavaScript
Kotlin WheelInput . ScrollOrigin scrollOrigin = WheelInput . ScrollOrigin . fromViewport ( 10 , 10 );
new Actions ( driver )
. scrollFromOrigin ( scrollOrigin , 0 , 200 )
. perform ();
scroll_origin = ScrollOrigin . from_viewport ( 10 , 10 )
ActionChains ( driver ) \
. scroll_from_origin ( scroll_origin , 0 , 200 ) \
. perform ()
var scrollOrigin = new WheelInputDevice . ScrollOrigin
{
Viewport = true ,
XOffset = 10 ,
YOffset = 10
};
new Actions ( driver )
. ScrollFromOrigin ( scrollOrigin , 0 , 200 )
. Perform ();
scroll_origin = Selenium :: WebDriver :: WheelActions :: ScrollOrigin . viewport ( 10 , 10 )
driver . action
. scroll_from ( scroll_origin , 0 , 200 )
. perform
await driver . actions ()
. scroll ( 10 , 10 , 0 , 200 )
. perform ()
val scrollOrigin = WheelInput . ScrollOrigin . fromViewport ( 10 , 10 )
Actions ( driver )
. scrollFromOrigin ( scrollOrigin , 0 , 200 )
. perform ()