Selenium: How to click at x,y coordinates?

  • I want to click at x, y coordinates like 52, 216-52, 295.

    How can this be done using Selenium? (I am using Mac OSX 10.8.5 with Selenium 2.40.0 and Firefox version 29.0)

  • You can move the mouse and click with Actions, given you have a element to offset from. You cannot click outside the browser content window or control Operating System elements.

    moveToElement(WebElement toElement, int xOffset, int yOffset)
    Moves the mouse to an offset from the top-left corner of the element.

    The code (Java) would look like this:

    Actions builder = new Actions(driver);   
    builder.moveToElement(knownElement, 10, 25).click().build().perform();

    Can you use the window itself as a known element so that you can click on an absolute x,y position? If so, how do you reference the window?

    @Doug I am not sure, but you could try the body element which should start in the upper left corner. If not please ask a new question, because its something new and different.

    If you want a web element that you can target which encompasses the entire screen, you could try the element that the , , and

    exist in. In the past I've used java's Robot library to move the curser to absolute screen positions on my monitor to perform actions. But it's a pretty messy solution since robot doesn't naturally distinguish where your browser window starts and ends.

    would there be an equivalent of this in NodeJS?

  • I needed to do this in javascript with protractor:

        , -20, -20  // pixel offset from top left

    i believe it should be `mouseMove(element(by.css('.material-dialog-container')), {x: -20, y: -20})`

  • Using MoveToElement you will be able to find or click in whatever point you want, you have just to define the first parameter, it can be the session(winappdriver) or driver(in other ways) which is created when you instance WindowsDriver. Otherwise you can set as first parameter a grid (my case), a list, a panel or whatever you want.

    Note: The top-left of your first parameter element will be the position X = 0 and Y = 0

    Actions actions = new Actions(this.session);
    int xPosition = this.session.FindElementsByAccessibilityId("GraphicView")[0].Size.Width - 530;
    int yPosition = this.session.FindElementsByAccessibilityId("GraphicView")[0].Size.Height- 150;
    actions.MoveToElement(this.xecuteClientSession.FindElementsByAccessibilityId("GraphicView")[0], xPosition, yPosition).ContextClick().Build().Perform();
  • Use swipe, except keep x and y locations same for start and end (and make duration really short).

    Eg. To touch (540, 905):

    driver.swipe(540, 905, 540, 905, 50);

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM