How to get exact y-coordinate from getLocation in selenium java?

4.5k Views Asked by At

Pre-req:

Selenium 3.141

Firefox browser

Requirement: Get x,y co-ordinates of an webelement and perform mouse move to xy co-ordinates. X is calculated properly whereas y co-ordinate is falling 100 pixel short.

Note: Webelement Formfield is hidden, user will perform vertical scroll and click on it. Co-ordinates are taken after scrolling.

WebElement fromfield = driver.findElement(By.xpath("//*[contains(@data-field-name,'deliverables')]"));
jse.executeScript("arguments[0].scrollIntoView();",fromfield); //scroll to the webelement, a small wait is given after scrolling

org.openqa.selenium.Point fromLocation = fromfield.getLocation();

int fromfield_x = fromLocation.x; 
int fromfield_y = fromLocation.y; //getx/gety returns same values

Actual Output: x = 550, y = 600
Expected Output: x = 550, y = 700. (**Note**: If I pass 700, then mouse moves correctly to required element, but here y is calculated incorrect)

Other trials:Tried with browser open in fullscreen mode but the same issue.

Queries:

How to get exact y co-ordinate?

Is xy co-ordinate calculated from left top corner of desktop window or viewport?


Update

Status:

Based on your suggestion, I tried below line of code and yes, it automatically scrolls to the required element.

WebElement source = fromfield.findElement(By.xpath(".//*[contains(@title,'test')]"));
 new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(source))).build().perform();

Outcome 1:

System.out.println("Y coordinate is: "+source.getLocation().getY());
int from_x = source.getLocation().getX();
int from_y = source.getLocation().getY();

I get y co-ords as 700 but the element might be at 900 pixels.

When I do mousemove, it moves to 700 pixels as returned, which is not the element to be dragged. My webelement (source) is still at 900 pixels. X co-ord is completely OK.

robot.mouseMove(from_x , from_y); //moves to 700 pixels
Actions maction=new Actions(driver);
Action drag = maction.clickAndHold(source).pause(3000).build();
drag.perform(); //tries to drag from 700 pixels

or

new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(source))).clickAndHold(source).build().perform();

Again y is falling short. What would be the reason?

Outcome 2: Above code snippet (movetoelement) works for chrome but not firefox.

1

There are 1 best solutions below

8
On

getLocation()

getLocation() returns the point where on the page is the top left-hand corner of the rendered element is located i.e. a point, containing the location of the top left-hand corner of the element.


As an example to extract the X and Y coordinates of the search box on Google Home Page you can use the following solution:

  • Code Block:

    driver.get("https://www.google.com/");
    WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.name("q")));
    Point elementLocation = element.getLocation();
    System.out.println("X coordinate of the element is: "+elementLocation.getX());
    System.out.println("Y coordinate of the element : "+elementLocation.getY());
    //or
    WebElement elem = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.name("q")));
    System.out.println("X coordinate is: "+elem.getLocation().getX());
    System.out.println("Y coordinate is: "+elem.getLocation().getY());
    driver.quit();
    
  • Console Output:

    X coordinate of the element is: 439
    Y coordinate of the element : 322
    X coordinate is: 439
    Y coordinate is: 322
    

Update 1

It is not that clear from your question why you want to perform mouse move to xy co-ordinates. However for optimum results you need to:

  • Induce WebDriverWait for the visibilityOfElementLocated() before you attempt to invoke scrollIntoView()

    • As an example:

      ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element"))));
      
    • You can find a relevant discussion in How to scroll UP to an element and click in selenium?

  • Induce WebDriverWait for the elementToBeClickable() before you attempt to interact i.e. invoke click()


Conclusion

As you mentioned ...perform vertical scroll and click on it..., the scrollIntoView() looks as a pure overhead as:

the following methods:

will automatically scroll the element within the Viewport.


Update 2

As per your comment to address the error regarding ...out of bounds of viewport... you can refer the discussion org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (x, y) is out of bounds while MouseHover with GeckoDriver Firefox Selenium


Update 3

As per your comment ...it moves to 700 pixels as returned, which is not the element to be dragged... it is worth mentioning that as I already mentioned within my answer that getLocation() returns the point where on the page is the top left-hand corner of the rendered element. Where as to perform a successful Drag and Drop operation, presumably the draggable element needs to be dragged up to certain percentage within the droppable element.

Additionally, if the html5 element to be dragged have the attribute draggable it needs to be handled differently which is a different topic altogether and we can discuss it in a seperate thread.

The relevant HTML would have helped us to a huge extent.


References

You can find a couple of relevant detailed discussion in: