How to click on (X, Y) coordinates using JavascriptExecutor in Selenium?

Many a time, we face an issue and we get an error message similar as- “Element is not clickable at point (X, Y)” So to solve such issues we have various techniques available in Selenium and Java, including which we are going to discuss today. We will discuss other techniques too in coming days, but let’s focus today on clicking on some XY coordinates using JavascriptExecutor in Selenium WebDriver.

This week we are only focussing on JavascriptExecutor tutorials in Selenium WebDriver. This is the last exclusive tutorial on JavascriptExecutor command; further, we will have a single powerful tutorial on JavscriptExecutor which will have the compilation of all the exclusive posts along with some additional commands which will be helpful to you achieving another milestone in your Selenium learning journey.

Don’t Miss:

How to send texts and retrieve texts using JavaScript?

Why element fails at certain coordinates, despite having right XPath?

Did you ever think why it fails even if we provided the right XPath to the element?

Let me tell you based on my analysis…

  • The testable browser has the older version
  • You are using outdated WebDriver, like ChromeDriver, GeckoDriver, etc
  • Element is disabled
  • By default, WebDriver clicks in the middle of the Web element, that is not active
  • Elements are not properly synced (Suggestions: Fluent wait works well for the Synchronization)

These could be the immediate reasons for the failure on click at certain coordinates.

If you have checked and corrected the above factors of failure then I expect that it will work well. If you haven’t done yet then I recommend you to get X & Y coordinates and click on each coordinates using JavascriptExecutor.

We don’t have any other choice, except, getting the coordinates and triggering the click operation on those specific web elements at particular (X, Y) coordinates using JavascriptExecutor.

click coordinates using javascriptexecutor

Let’s begin the game….

Click on (X, Y) coordinates using JavascriptExecutor in Selenium WebDriver

I recommend you to update your browser first then try to separately click at X and Y coordinates using JavascriptExecutor.

Click on X coordinates using JavascriptExecutor, while keeping Y coordinates constant

Here is the command to click at X coordinates while keeping the Y coordinate constant.

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

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(“window.scrollTo(0, ele.getLocation().x+”)”);

ele.click();

Here is the complete program:

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.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class JavascriptCoordinatesClick {
  @Test
  public void javascriptCoordinatesClick() throws InterruptedException {

	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	 
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("https://www.inviul.com");
	  
	  driver.manage().window().maximize();
	  Thread.sleep(3000);
	  
	  WebElement ele = driver.findElement(By.xpath("(//*[@class='sf-with-ul'])[5]"));
	  Thread.sleep(3000);
	  
	  //Javascript command
	  JavascriptExecutor js = (JavascriptExecutor)driver;
	  js.executeScript("window.scrollTo(0, "+ele.getLocation().x+")");	  
	  ele.click();
	  Thread.sleep(5000);
	  
	  driver.close();
	  
	  driver.quit();


  }
}

Explanations

WebDriver scrolls down till it reaches the X coordinates and then clicks.

Click on Y coordinates using JavascriptExecutor, while keeping X coordinates constant

 

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

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(“window.scrollTo(0, ele.getLocation().y+”)”);

ele.click();

Here is the complete program.

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.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class JavascriptCoordinatesClick {
  @Test
  public void javascriptCoordinatesClick() throws InterruptedException {

	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	 
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("https://www.inviul.com");
	  
	  driver.manage().window().maximize();
	  Thread.sleep(3000);
	  
	  WebElement ele = driver.findElement(By.xpath("(//*[@class='sf-with-ul'])[5]"));
	  Thread.sleep(3000);
	  
	  //Javascript command
	  JavascriptExecutor js = (JavascriptExecutor)driver;
	  js.executeScript("window.scrollTo(0, "+ele.getLocation().y+")");	  
	  ele.click();
	  Thread.sleep(5000);
	  
	  driver.close();
	  
	  driver.quit();


  }
}

Explanations

WebDriver scrolls till it reaches theY coordinates and then clicks.

This is all about clicking at (X, Y) coordinates using JavascriptExecutor in Selenium WebDriver. There are many ways to click on particular coordinates. Stay connected so that you can read those tutorials too. If you find any doubts, or, if you have some suggestions to make this portal better, then feel free to reach to me via comment, contact page, and social media.

This is Avinash Mishra, signing off!

3 Comments

Leave a Reply