0

I am trying to get the current coordinates of the cursor in selenium, basically because I want to move it to an element, BUT, very important, I am not looking forward to move it with action chains to the element directly, I want it to do a smooth trajectory, but to make this possible, I need to understand:

  1. Can I get the current cursor position to calculate trajectory?

or!!

  1. is there a way to move a "somehow" generated cursor into a trajectory to an element?

NOTE: I don't want this solution

webdriver.ActionChains(self.driver).move_to_element(element).click(
                element).perform()

I need something that is more similar to the following:

def element_coordinates(e):
    location = e.location
    print(location)
    return location


e = WebDriverWait(self.driver, 5).until(
            EC.element_to_be_clickable((By.XPATH, '//div[contains(@label, "element")]')))
    
coordinates = element_coordinates(e)

Gives me the following output for a specific element. The following is the kind of output I want to have for the cursor:

>>> {'y': 20, 'x': -65}
The Dan
  • 1,408
  • 6
  • 16
  • 41
  • Or this: https://stackoverflow.com/questions/21477124/method-for-getting-position-of-the-cursor-in-web-driver – Prophet Jul 06 '21 at 20:45

1 Answers1

1

I have used this method long back. It is in JAVA see if you can implement the same in python.

public int[] GetElementCoordinateWithId(String elementId) {
    int[] Location = new int[2];
    WebElement myElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(elementId)));
    Point location = myElement.getLocation();
    Location[0] = location.getX();
    Location[1] = location.getY();
    System.out.println("Coordinates of an element is : " + Location[0] + " and " + Location[1]);
    return Location;
  }

Do let me know if you need more explanation.

Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
  • I hacve a question here. How do I get the "cursor" element? I understand that if I select an element of the page, it will be easy to get those coordinates, but for the cursor is different – The Dan Jul 08 '21 at 20:04