GroTechMinds

Can a ‘WebElement’ be left-clicked in four different ways

Can a ‘WebElement’ be left-clicked in four different ways?

Yes, it can.

Performing automated left-clicks on a ‘WebElement’ was a game changer for a Software tester. The tedious task of clicking from one link to another, for test case upon test case, is one of the mind-numbing repetitive tasks manual testers have to perform. We can left-click any ‘WebElement’ with precision and accuracy using the various ways of performing clicks that are explained below.

What is a ‘WebElement’?

‘WebElement’ is an Interface that comes from the ‘org.openqa.selenium’ package. It represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this interface. It has many methods to facilitate the locating of a ‘WebElement’ and Interaction with it.

The various methods by which we can simulate the left-click action of a mouse are as follows.
Method 1:

After locating the WebElement that must be clicked, we can use the ‘click’ method of the ‘WebElement’ Interface to perform a left-click on the desired ‘WebElement’.

Code Snippet:
				
					public static void main(String[] args)
   	{
          	WebDriver driver = new ChromeDriver();
          	driver.get("https://www.google.com");
          	driver.manage().window().maximize();
          	WebElement menu = driver.findElement(By.className("gb_d"));
          	menu.click();
          	driver.quit();
   	}

				
			
Method 2:

Using the ‘sendKeys’ method of the ‘WebElement’ Interface, we can perform actions that can be performed on a physical keyboard to the desired ‘WebElement’. Using an enum class called ’Keys’ and by utilizing its constants we can perform a left-click on the desired ‘WebElement’.

Code Snippet:
				
					public static void main(String[] args)
   	{
          	WebDriver driver = new ChromeDriver();
          	driver.get("https://www.google.com");
          	driver.manage().window().maximize();
          	WebElement search_box = driver.findElement(By.name("q"));
          	search_box.sendKeys("grotechminds");
          	search_box.sendKeys(Keys.DOWN);
          	search_box.sendKeys(Keys.ENTER);
          	driver.quit();
   	}

				
			
Method 3:

By the use of the ‘Actions’ class and its method called ‘moveToElement‘ we can hover over a desired ‘WebElement’ and perform ‘left-click’ using the ‘click’ method of the ‘Actions’ class.

Code Snippet:
				
					public static void main(String[] args)
   	{
          	WebDriver driver = new ChromeDriver();
          	driver.get("https://www.google.com");
          	driver.manage().window().maximize();
          	WebElement menu = driver.findElement(By.className("gb_d"));
          	Actions a = new Actions(driver);
          	a.moveToElement(menu).click().perform();
          	driver.quit();
   	}

				
			
Method 4:

This method of using the ‘JavaScriptExecutor’ Interface is not very popular, but it can be another useful tool for any automation tester. Here, with the object of the ‘JavascriptExecutor’ Interface, and using one of its methods called as ‘executeScript’, we can perform left-click on the desired ‘WebElement’.

Code Snippet:
Starting Out: Entry-Level Roles and Expectations
				
					public static void main(String[] args)
   	{
          	WebDriver driver = new ChromeDriver();
          	driver.get("https://www.google.com");
          	driver.manage().window().maximize();
          	WebElement menu = driver.findElement(By.className("gb_d"));
          	JavascriptExecutor jsE=(JavascriptExecutor) driver;
          	jsE.executeScript("arguments[0].click();", menu);
   	}

				
			

P.S. ‘arguments[0]’ executes the first argument, which is executing the ‘click’ method on the WebElement ‘menu’.

Conclusion

Getting to know the various methods that can used to perform a single ‘left-click’ on a desired ‘WebElement’ provides us with knowledge that could be an important addition to our skill repository. It is very handy to know all the possible methods for simulating the clicking of the left mouse button, so as a tester, we can be prepared.

Upskill Yourself
Consult Us