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.