Selenium Click Commands Explained With Examples

Selenium Click Commands Explained With Examples

Why are click commands so useful?

Click commands are the foundation of any automated test. They are used to tell the driver to click on various on-screen items as the test is being executed. They are the first thing any tester needs to know how to use when beginning their journey into test automation.

This article will cover left, right and double clicks.

Left clicks

The simplest of the click commands, the left click, is provided by most if not all WebDrivers out-of-the-box. Here you can see an example of the left click in selenium:

WebElement searchConfirmButton = driver.findElement(By.xpath("//fieldset//button"));;

If you want to see the case being run with a live video, click here:

Run Working Example

Other Clicks

Sometimes when going through a test scenario you will find that a left click is not sufficient.

For example, you may have some functionality that is only revealed by a right click (usually known as a context click in test automation). Or, you could come across a Javascript function embedded in the page that only executes after a double click.

Both of these examples are handled by the Actions class in Java Selenium.

Right Clicks

Here is an example of a right-click:

WebElement searchConfirmButton = driver.findElement(By.xpath("//fieldset//button"));

Actions actionsDriver = new Actions(driver);

Here is a modified version of the runnable case you saw before, except this time the test will right-click the search button before left-clicking it. There is a long pause between these actions so you visually verify that this code works.

Run Working Example

Double Clicks

Here is an example of a double click:

WebElement subTitle = driver.findElement(By.xpath("//h1//strong"));

Actions actionsDriver = new Actions(driver);

This example is also from a modified version of the first runnable case you saw in this article.

Here is a runnable case that uses the code snippet to highlight a portion of Wikipedia's subtitle, pausing after to show the result in the live video feed.

Run Working Example

Leave a reply

Your email address will not be published. Required fields are marked*

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.