Introduction to Right Click and Double Click in Selenium

Two crucial user behaviors for interacting with a website are Introduction to right and double clicks. In Selenium Test Automation, it becomes necessary to carry out these operations. You can check out the Selenium course to learn more.
Right click: What is it?
As the name implies, the context menu appears when a user attempts to click the Right mouse button on a website or a web element to view it.
E.g. This is a pretty typical action that is practically always utilised, particularly when using Windows Explorer’s folders. Similarly to this, when you right-click any email in your inbox in Gmail, a menu containing options like Reply, Delete, Reply All, etc. appears. Any option from which the desired action can be carried out can be chosen.
Automation test scripts frequently require the same action, i.e., right-clicking on a web page element and choosing an option from the context menu that appears. WebDriver API lacks the ability to conduct the right-click action through a Selenium script, unlike other Action commands like click and sendKeys.
Action class fills this need by offering a number of crucial methods to mimic human actions. ContextClick(WebElement), one of the class’s most frequently used methods, is used to execute the Right-Click operation.
Right-click in Selenium?
Let’s see how to right-click using Action class methods.
First, instantiate an Actions class:
Actions actions = new Actions(driver);
Now, as can be seen above, the contextClick() method accepts the argument WebElement. Thus, a WebElement object must be passed to the procedure. The button or any other web element on which we want to conduct a right click should be this WebElement. Use the following command to locate the element:
WebElement webElement = driver.findElement(Any By strategy & locator);
To discover the WebElement in this case, you can use any By technique, such as finding an element by its id or name property. Please refer to the Locate Elements lesson for further information on all By methods.
Now that we have the element and the action class object, we simply need to use the perform() method for the right-click:
actions.contextClick(webElement).perform();
Let’s see what happens internally when invoking the perform() method above:
Move to Element: The contextClick() method first moves the mouse to the centre of the element. The centre of the web element is clicked using this function.
Build: To create a composite action that contains all actions, use the build() method. Nevertheless, as you can see, we did not invoke it in our aforementioned command. Internally, the perform method runs the build.
Perform: The perform() method carries out the provided activities. But first, it internally calls the construct() method. The build is followed by the action.
- Open the browser and navigate to a decent practice page.
- Locate the necessary element, which in our example is the button, and right-click it.
- Go to the ‘copy’ option and choose it.
- Accept the alert message
- Close the browser to end the program
What is a Double click?
Double-clicking is a common user activity. File Explorer is where double-clicking is most frequently used. Any folder or file within a folder can be opened in File Explorer by double-clicking on it.
Similarly to this, some items on any webpage could need two clicks to initiate a specific action. Similar to the Right Click, Selenium does not provide a WebDriver API command that can double-click a web element.
So, to carry out this user action, the Action class function doubleClick(WebElement) must be utilised.
Double-click in Selenium
Let’s look at how to double-click using the Actions class methods:
First, let’s instantiate an Actions class
Actions actions = new Actions(driver);
Now, as can be seen above, the doubleClick() method accepts the argument WebElement. Thus, send a WebElement object to the procedure that requires a double click.
WebElement webElement = driver.findElement(Any By strategy);
Use any By approach, such as finding element by its id, name attribute, etc., to locate the WebElement, exactly like you would with a Right-Click.
Now, just invoke build and perform for our double click
actions. DoubleClick (webElement).perform();
The Move to Element >> Construct >> Perform sequence is followed by the doubleClick() method just like it is for right-clicking.
- Open the browser and navigate to a decent practice page.
- Locate the necessary element, which in our example is the button, and double-click it.
- Embrace the alert message
- To end the software, close the browser.
Conclusion
In conclusion, we have shown how Action Class methods can be used to perform Right Click and Double Click on-site items. The Actions class provides additional ways to carry out different keyboard events. A good Selenium course online will explain both concepts.