qatestprofile
qatestprofile

Reputation: 133

Get the On Screen location of WebElement using JAVA

One Liner - How do I get the onscreen coordinates of a WebElement using Java / Selenium

I have an element on the page which keeps floating around at random positions, I would like to click on the text box using Selenium ( which is achievable using findElement(By.id("btn")).click ) but rather I want to use the Robot Class over here and move to the the particular WebElement using its coordinates.

The problem I am facing is I am not able to get the "on screen" coordinates for the WebElement dynamically which makes it harder for me use the Robot Class

I have tried using the Point class

Point coordinates = driver.findElement(By.id("btn")).getLocation();
int x = coordinates.getX();
int y = coordinates.getY();

But if I use the values of these in the Robot class using the below method the pointer moves to a location a little further than the given coordinates and that seemingly is because the current coordinates are at the browser level whereas the mouseMove method is at on screen level

Robot robot = new Robot();
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);

The below is a snippet of how the page works and you can also download the HTML file if you'd like to play with it

var btn = document.getElementById("btn");
btn.style.top = Math.floor((Math.random() * 230) + 1) + "px";
btn.style.left = Math.floor((Math.random() * 200) + 1) + "px";
input[type='text'] {
       position: absolute;
  }
<input placeholder="Click Here to Type Something" type="text" id="btn" name="fname">

Upvotes: 2

Views: 1294

Answers (1)

Tim Lavers
Tim Lavers

Reputation: 11

As you've pointed out, the root of the problem is that the getLocation() method returns a value relative to the top left corner of the page, but you don't know where that is in screen coordinates.

If your browser is maximised, then the problem is a bit simpler as you know where on the screen the top of the browser is. In full screen mode, things are simpler still as the browser tabs and address bar are out of the way.

In Chrome, in automated tests, the only thing between the top and the page and the screen is the "Chrome is being controlled by automated test software." warning bar.

So one approach to the problem is to enter full screen mode: driver.manage().window().fullscreen(); and then use a constant y offset, which you can find by trial and error, in your location calculations.

Upvotes: 1

Related Questions