Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find "Cursor" position in webpage with Selenium Webdriver?

Tags:

java

selenium

I just started learning Selenium Webdriver using java Script, the question what I have asked might me a silly one. But still wanted to know whether it is possible or not.

As per my question, please go through the below example

In any Login page, enter valid "Username" and click on "signin" button, it throws an error message "Please enter password" and the "cursor" will be located in "Password" field

So, we can get the error message via code. Now, how can we locate or identify the "Cursor" position in the webpage via code?

like image 653
user3313128 Avatar asked Feb 15 '14 11:02

user3313128


People also ask

How does Selenium validate cursor position?

Selenium will right click on your element, which will result in the context menu being displayed at the bottom right corner of your cursor. Once you see the position of the context menu, you will know whether Selenium is clicking on the right element.

How can I track my cursor position?

Once you're in Mouse settings, select Additional mouse options from the links on the right side of the page. In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK. To see it in action, press CTRL.

How do you check the position of an element in Selenium?

We can use following two commands to verify the presence of an element: verifyElementPresent – returns TRUE if the specified element was FOUND in the page; FALSE if otherwise. verifyElementNotPresent – returns TRUE if the specified element was NOT FOUND anywhere in the page; FALSE if it is present.

How do I find the coordinates of a website element?

To get the unique coordinates of an element we shall create an object of the class Point which shall store the location of the webelement obtained from the getLocation method. Then the individual x and y coordinate values can be computed from the getX and the getY methods respectively.


2 Answers

By definition, any widget that has focus also has the cursor. So driver.switchTo().activeElement() will return the currently focused WebElement. It doesn't actually change anything, just returns the active element. You can call

expectedElement.equals(driver.switchTo().activeElement());

to verify that expectedElement has focus.

like image 195
MikeJRamsey56 Avatar answered Sep 25 '22 21:09

MikeJRamsey56


Hey user3313128 with Selenium Web driver library it is only possible to figure out the current position by knowing the last place you moved it to.

Javascript sadly dose not give you an option to quickly get the X & Y.

An easy work around this is to simple run an simple function in the browser

async function (){
    await this.driver.executeScript(function(  ) { 
        let mouse = { x:0, y:0 }
        onmousemove = function(e){ mouse.x = e.clientX, mouse.y = e.clientY }
    })
}

this function will track your mouses X and Y in the browser the whole time

Then when you need to know mouse's location just call this script

mouse = driver.executeScript( function (){return mouse}) 

although the smart move is to track all of this within your JS on the server side at each mouse move and click().

like image 29
Peter the Russian Avatar answered Sep 22 '22 21:09

Peter the Russian