How to Handle Mouse Operations in Selenium.

Posted By : Mohd Nawaz Hussain | 27-Dec-2022

Mouse operations are very important while testing an application with Selenium. Selenium provides a user with a very efficient way to handle various mouse operations.

The different types of mouse operations which can be performed using Selenium are:

  • Clicking on an element
  • Double clicking on an element
  • Right clicking on an element
  • Hovering over an element
  • Dragging and dropping an element

Let us see how to perform each of these operations one by one.

Clicking on an element:

Clicking on an element can be performed using the click() method. This method takes a WebElement as an argument and clicks on it.

For example, if we want to click on a button with the text "Click me", we can use the following code:

button.click();

Double clicking on an element:

Double clicking on an element can be performed using the doubleClick() method. This method also takes a WebElement as an argument and double clicks on it.

For example, if we want to double click on a link with the text "Click me", we can use the following code:

link.doubleClick();

Right clicking on an element:

Right clicking on an element can be performed using the contextClick() method. This method also takes a WebElement as an argument and right clicks on it.

For example, if we want to right click on a button with the text "Click me", we can use the following code:

button.contextClick();

Hovering over an element:

Hovering over an element can be performed using the hover() method. This method takes a WebElement as an argument and hovers the mouse over it.

For example, if we want to hover over a link with the text "Hover me", we can use the following code:

link.hover();

Dragging and dropping an element:

Dragging and dropping an element can be performed using thedragAndDrop() method. This method takes two WebElements as arguments. The first WebElement is the element to be dragged and the second WebElement is the element to be dropped on.

For example, if we want to drag and drop a button with the text "Drag me" onto a link with the text "Drop me", we can use the following code:

button.dragAndDrop(link);

 

Related Tags

About Author

Author Image
Mohd Nawaz Hussain

Mohd possesses more than 1 year of experience as a Quality Analyst. He has good knowledge in both automation and manual testing. He has hands-on experience in Design Automation Frameworks and Automation Test Scripts.

Request for Proposal

Name is required

Comment is required

Sending message..