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.
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:
Console Output:
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 invokescrollIntoView()
As an example:
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. invokeclick()
As an example:
You can find a relevant discussion in How to mouse hover a parent element and subsequently click on child element using Selenium and Action class
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: