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?
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.
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.
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.
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.
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.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With