20

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)

4 Answers 4

9

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();
7
  • 3
    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
    Sep 13, 2014 at 23:21
  • 1
    @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. Sep 14, 2014 at 8:52
  • 1
    If you want a web element that you can target which encompasses the entire screen, you could try the <html> element that the <head>, <body>, and <footer> 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.
    – Julian
    May 13, 2015 at 19:36
  • would there be an equivalent of this in NodeJS? Jul 13, 2018 at 15:08
  • 1
    @ddsultan They moved it selenium.dev: selenium.dev/selenium/docs/api/javascript/module/… Dec 8, 2020 at 14:05
3

I needed to do this in javascript with protractor:

  browser.actions()
  .mouseMove(
    element(by.css('.material-dialog-container'))
    , -20, -20  // pixel offset from top left
  )
  .click()
  .perform();
1
  • 2
    i believe it should be mouseMove(element(by.css('.material-dialog-container')), {x: -20, y: -20})
    – Dziamid
    Apr 2, 2015 at 10:25
1

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();
0

Use swipe, except keep x and y locations same for start and end (and make duration really short).

E.g.: to touch (540, 905):

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.