22

I was expecting something like WebElement.isfocus(),... something really simple, but the only approach I found is using the

:focus 

pseudo class.

Is this really such an uncommon task as for not find tons of information?

I'm aware of this SO topic, but it's been almost two years from then. Nothing new in more recent versions?

Would you know some workaround?

Community
  • 1
  • 1
luso
  • 2,812
  • 6
  • 35
  • 50

2 Answers2

39

There is another topic that covers this issue: Test if an element is focused using Selenium Webdriver

Basically the code will be

element.equals(driver.switchTo().activeElement());
Community
  • 1
  • 1
JacekM
  • 4,041
  • 1
  • 27
  • 34
8

for python developers:

def is_element_focus(id):
    return self.driver.find_element_by_id(id) == self.driver.switch_to.active_element
Bartłomiej Bartnicki
  • 1,067
  • 2
  • 14
  • 30