2

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.

sridattas
  • 459
  • 1
  • 6
  • 21

1 Answers1

3

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks. I'm able to get the x and y co-ordinated but the y co-ordinates is incorrect. It is 100pixel short. How to get exact y co-ordinate? – sridattas Oct 24 '19 at 11:42
  • @sridattas Checkout the updated answer and let me know the status. – undetected Selenium Oct 24 '19 at 12:45
  • @DebanjanB.Thanks again.I tried replacing scrollintoview with movetoelement and elementtobeclickable as suggested and I get exception:(517, 853.25) is out of bounds of viewport width(1898)and height(829),no scroll happens.Usecase is to perform dragdrop of element and the element is hidden, hence I need to scroll. I tried all other actions of dragdrop but didn't work since its an html5 dragdrop. I also tried solution provided by rcorreia. Now I'm trying with mousemove/press/release which works with hardcoded xy coordinates.but,i try to pass co-ords dynamically, x is OK but y is falling short. – sridattas Oct 24 '19 at 13:07
  • **html5 dragdrop** is the key consideration here which you may like to ask as a separate question as that would be different story altogether. – undetected Selenium Oct 24 '19 at 13:24
  • @sridattas Check out the updated answer and let me know the status. – undetected Selenium Oct 24 '19 at 13:34
  • Updated my status and outcome. y seem to be still falling short. Scroll is now working with movetoelement. Can you please let me know why y coords is incorrect? Thanks. – sridattas Oct 25 '19 at 10:38
  • @sridattas Checkout the answer update and let me know the status. – undetected Selenium Oct 25 '19 at 10:41
  • 1
    Perfect Solution +1 – Ahtisham Ilyas Aug 25 '22 at 12:30