SELENIUM BASICS:


SELENIUM WEBDRIVER:


TESTNG:


REPORTS:


BUILD & CONTINUOUS INTEGRATION TOOLS:


CROSS BROWSER TESTING:


MOBILE TESTING:


AUTOMATION FRAMEWORKS:


API Testing:


INTERVIEW QUESTIONS:


SAMPLE RESUME:

How To Handle Mouse and Keyboard Interactions in Selenium Python

In the previous articles on Selenium Python Tutorial, we have covered “How To Handle Child Window, Frames, Alerts in Selenium Python“. In this tutorial, we will learn How To Handle Mouse & Keyboard Interactions in Selenium Python. 

The mouse and keyboard movements can be simulated with the help of Selenium WebDriver. The actions like a double click, right-click, mouse movement, key press, mouse button connection, hovering and drag and drop and so on are performed with the help of ActionChains class in Selenium.

While we are using methods of ActionChains object, all the actions are kept in a queue. After executing the perform () method, all the actions are triggered one by one in a sequence. The actions can be queued up one after the other or placed in a chained order.

Syntax with queueing the actions.

Syntax with chaining the actions.

Let us see some of the mouse actions methods under ActionChains:

click (args) – This method shall click on a web element. The argument args is the web element to be clicked. If the parameter to the method is ignored, then the current mouse position is clicked.

click_and_hold (args) – This method shall click on the current mouse location without releasing it. The argument args is the web element to mouse down. If the parameter to the method is ignored, then the current mouse position is clicked.

double_click (args) – This method shall double click on a web element. The argument args is the web element to be double-clicked. If the parameter to the method is ignored, then the current mouse position is double-clicked.

context_click (args) – This method shall right click on a web element. The argument args is the web element to be right-clicked. If the parameter to the method is ignored, then the current mouse position is right-clicked.

drag_and_drop (s, d) – This method is used to hold down the mouse from the element at the source, then move to the destination element and finally release the mouse. The argument s refers to the location of the source element to mouse down and the argument d refers to the location of the destination to mouse up.

drag_and_drop_by_offset (s, offsetX, offsetY) – This method is used to hold down the mouse from the element at the source, then move to the destination element offset, and finally release the mouse. The argument s refers to the location of the source element to mouse down. The argument offsetX refers to the X offset to move to. The argument offsetY refers to the Y offset to move to.

move_to_element (args) – This method is used to move to the middle of the web element. The args is the web element to be moved to.

release (args) – This method is used to release a held mouse button on a web element. The argument args is the web element to mouse up. If the parameter to the method is ignored, then the current mouse position is released.

Let us see some of the keyboard methods under ActionChains:

send_keys (keys) – This method is used to send keys to the present active element. The argument keys are keys to send. The modifier keys are available from the Keys class.

key_down (v, args) – This method is used to do a key press without releasing it. This method can be used only with the Control, Alt, and Shift modifier keys. The argument v is a modifier key available from the Keys class. The argument args is the web element to send keys. If that parameter to the method is ignored, it sends keys to the present active element.

key_up (v, args) – This method is used to release a modifier key. The argument v is a modifier key available from the Keys class. The argument args is the web element to send keys. If that parameter to the method is ignored, it sends keys to the present active element.

Code Implementation for mouse hover action.

Code Implementation for drag and drop action.

Code Implementation for right-clicking action.

Code Implementation for double-clicking action.

Code Implementation for clicking action.

Code Implementation for key up and down action.

In the next article, we will learn Assertions in Selenium Python

Related posts:

Sharing is caring.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar

Leave a Comment

API Testing eBook

DOWNLOAD FOR FREE