Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to click using JavascriptExecutor in Selenium?

People say, “Old is Gold”. They rightly said this because when all the general commands fail to perform its operation then Javascriptexecutor comes into the picture and clears the blockages. You know the initial release of Selenium was completely based on JavaScript. Today we are going to learn the click using JavascriptExecutor.

You may face some situations in Selenium where click operations do not go well. There could be many reasons; some of the reasons are listed below:

  • On –click method triggers JavaScript code
  • A clickable element is disabled

In this scenario, exceptions like, IllegalStateException, ElementNotVisibleException, StaleElementReferenceException and NoSuchElementException comes. This is the beauty of JavaScript that it has complete authority over the DOM hierarchy. We can execute any of the scripts using JavascriptExecutor which helps us to handle the adverse scenario of the Automation testing.

Recently, one of my friends had a problem in sending the text to the text box in Mozilla browser; however, the normal command was working fine in Chrome browser. He called me to seek help. I advised him to use JavaScript to send the text to Mozilla browser and believe me it worked. So we will later discuss this scenario in a separate post. Let’s discuss the scenario and command to click using JavascriptExecutor in Selenium Automation tool.

Before we proceed ahead I have some hot tutorials for you on Selenium Automation testing. Please have a look, it may benefit you a lot:

Awesome Articles for You

  • How to scroll in Selenium using JavaScript?
  • Advanced dynamic XPath in Selenium to reduce the failure rate
  • Fluent wait and its uses
  • Explicit wait and its uses
  • Implicit wait and its uses
  • How to disable developers mode extension pop up warning?
  • How robot class handles human interactions in Selenium
  • How to verify the title of any web page?
  • How to verify the error message in Selenium?

So these are really some awesome articles which you will love it definitely, It also increases your Selenium’s knowledge bank.

Let’s come back to our main topic. 🙂

click using javascriptexecutor in selenium

Performing click using JavascriptExecutor in Selenium Automation tool

Our approach to this scenario would be to first identify the type of exceptions which Selenium is sending to the console. This exception should come from the script from where this was getting failed. Once we identified the spot then further we have to handle the spot with JavaScript and we can do it by creating the instance of JavascriptExecutor.

Here is the basic and sample JavaScript command to click on the Web elements:

WebElement webl = driver.findElement(By.xpath(“xpath_expression”));

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript(script, webl);

Let’s discuss this technique and following is the sample command for it.

WebElement webl = driver.findElement(By.xpath(“xpath_expression”));

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript(“arguments[0].click();”, webl);

Here is the entire program for performing click using JavascriptExecutor in Selenium Automation tool:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class JavascriptClickDemo {
  public void javaScriptClickDemo() throws InterruptedException {
 System.setProperty("", "C:\\Selenium\\chromedriver.exe");
	  WebDriver driver = new ChromeDriver();
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  WebElement webl = driver.findElement(By.xpath("(//*[@class='sf-with-ul'])[5]"));
	  //Javascript command
	  JavascriptExecutor js = (JavascriptExecutor)driver;

The post How to click using JavascriptExecutor in Selenium? appeared first on Inviul.

This post first appeared on Inviul, please read the originial post: here

Share the post

How to click using JavascriptExecutor in Selenium?


Subscribe to Inviul

Get updates delivered right to your inbox!

Thank you for your subscription