I'm writing an automation testing framework using Selenium. I am trying to Locate which element my cursor is pointing to.

for eg. there are two textbox textbox1 and textbox2. If I perform a tab swift operation in java to move focus form textbox1 to textbox2 such that now textbox2 is at focus and the cursor position is moved, so is their any way to find in java whether the focus is actually shifted or not..

I usually try finding element such as,id,name,css,xpath.eg

driver.findElement(By.id("login_error")).getText().equals("Please enter Valid Data")

but finding by cursor is a new challenge for me which I am not getting through.
thanks in advance

  • 1
    What code you have tried , please share that so can check and correct it. – Helping Hands Feb 19 '15 at 6:49
  • I wish you could downvote comments like the one above. – boatcoder Jun 1 '18 at 22:03

To find out if a specific element has focus, call element.equals(driver.switchTo().activeElement())

More info in this topic.

  • I want to find the id or name of the element on focus, such that [id="textbox2" equals to activeElement].... – Rajesh Rathod Feb 19 '15 at 7:57
  • @Val then you can just try driver.findElement(By.Id("textbox2")).equals(driver.switchTo().activeElement()) – FDM Feb 19 '15 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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