How To Find Web Elements X Y Coordinates Using Selenium WebDriver
Finding Web Elements X Y Coordinates Using Selenium:
Any web element has its own position on page known as x y coordinates. x y coordinates of a web element is measured in x and y pixels. x pixels means the horizontal position of an element on a page from the left side and y pixels means the vertical position of an element on a page from the top. Let’s see how to find web elements x y coordinates using Selenium WebDriver.
By using Point class we could get the web elements x y coordinates in Selenium WebDriver.
Also Read: SQL Tutorial for Software Testers
Let’s see a practical example on how to find web elements x y coordinates using Selenium WebDriver:
Given clear explanation in the comments section within the program itself. Please go through it to understand the flow.
package softwareTestingMaterial;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class TestTestTest {
public static void main (String [] args) throws InterruptedException {
//Instantiation of driver object. To launch Firefox browser
System.setProperty("webdriver.chrome.driver", "D:\\Selenium Environment\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//To open url
driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/");
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(5000);
driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/");
// find the SoftwareTestingMaterial logo using linkText Locator
WebElement element = driver.findElement(By.linkText("Software Testing Material"));
//Used points class to get x and y coordinates of element.
Point point = element.getLocation();
int xcord = point.getX();
System.out.println("Position of the webelement from left side is "+xcord +" pixels");
int ycord = point.getY();
System.out.println("Position of the webelement from top side is "+ycord +" pixels");
// Using Actions class
Actions action = new Actions(driver);
//clicking on the logo based on x coordinate and y coordinate
//you will be redirecting to the home page of softwaretestingmaterial.com
action.moveToElement(element, xcord, ycord).click().build().perform();
/*((JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+element.getLocation().y+")");
element.click();*/
/*JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("window.scroll(" + xcord + ", " + ycord + ");");
executor.executeScript("arguments[0].click();", element);*/
}
}
Execute the above script and see the console output. You could see the values of x and y coordinates.
Also, see how to get the size of an element in Selenium WebDriver:
To get the width and height of an element, I have used getSize() method
Practical Example:
Given clear explanation in the comments section within the program itself. Please go through it to understand the flow.
package softwareTestingMaterial;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestTestTest {
public static void main (String [] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium Environment\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/");
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(5000);
driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/");
// find the SoftwareTestingMaterial logo using linkText Locator
WebElement logo = driver.findElement(By.linkText("Software Testing Material"));
//To get the width of the logo
int logoWidth = logo.getSize().getWidth();
System.out.println("Logo width : "+logoWidth+" pixels");
//To get the height of the logo
int logoHeight = logo.getSize().getHeight();
System.out.println("Logo height : "+logoHeight+" pixels");
}
}
Execute the above script and see the console output. You could see the width and height of the logo.
If you are not a regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.

how do you find an element using the x,y coordinates ?
Hi Hema, Use the below code..
Use Action class and pass x, y coordinates
Actions action = new Actions(driver);action.moveToElement(element, xcord, ycord).click().build().perform();