JavaScript How to get the x, y coordinates of an element in a web page?

I am using Selenium WebDriver and coding in Java. In a code I need to scroll down to a specific element in the web page for clicking it. I am using JavascriptExecutor command. My question is how will I get to know the exact x and y coordinates of that specific element as per the position of it in the web page. The syntax of the code I am using is given below:

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("scroll(x,y)");

In the second line of the above code I need to give the specific values for the x and y coordinates of the element I want to click.

Answer:1

I suggest you to give reference the element itself instead of its coordinates.

((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", element);

Hope this helps. Thanks.

Answer:2

You can get coordinates using java selenium,

webElement.getLocation().getX();
webElement.getLocation().getY();
Answer:3

To know the exact x and y coordinates of that specific element as per the pixel position of it in the web page you can consider to use the following code block:

import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class location_of_element 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.co.in");
        WebElement element = driver.findElement(By.name("q"));
        Point point = element.getLocation();
        System.out.println("Element's Position from left side is: "+point.getX()+" pixels.");
        System.out.println("Element's Position from top is: "+point.getY()+" pixels.");
     }
}

Ensure that you have imported org.openqa.selenium.Point

The output on your console will be as:

Element's Position from left side is: 413 pixels.
Element's Position from top is: 322 pixels.
Answer:4

I'd like to bind 2 Angular model variables to a range input element according to the following: update first model variable for every change of the range input update second model variable after x ms ...

I'd like to bind 2 Angular model variables to a range input element according to the following: update first model variable for every change of the range input update second model variable after x ms ...

  1. input bind variables

Given two points: const point1 = {x: 100, y: 0.95} const point2 = {x: 75, y: 1.05} I need to know the value of y when x equals some arbitrary value. Please assume a linear relationship exists. For ...

Given two points: const point1 = {x: 100, y: 0.95} const point2 = {x: 75, y: 1.05} I need to know the value of y when x equals some arbitrary value. Please assume a linear relationship exists. For ...

  1. function point line of code
  2. point function line
  3. function point and line of code difference

I am trying to get an input box to be disabled when a checkbox is checked. I'm trying to use Knockoutjs to get this done, but it doesn't seem to work. Here is my html: <input id="input1" type="...

I am trying to get an input box to be disabled when a checkbox is checked. I'm trying to use Knockoutjs to get this done, but it doesn't seem to work. Here is my html: <input id="input1" type="...

  1. knockout js disabled binding
  2. knockout js disable binding

I'm trying to send characters to an input element based on user actions. I'm trying to use KeyboardEvent with dispatchEvent but whatever I do, it doesn't work For example: let keyEvent = new ...

I'm trying to send characters to an input element based on user actions. I'm trying to use KeyboardEvent with dispatchEvent but whatever I do, it doesn't work For example: let keyEvent = new ...