Selenium:检查WebElement是否具有焦点

lus*_*uso 20 selenium selenium-webdriver

我期待像WebElement.isfocus()这样的东西......真的很简单,但我找到的唯一方法是使用

:focus 
Run Code Online (Sandbox Code Playgroud)

伪类.

这真的是一个不寻常的任务,因为没有找到大量的信息?

我知道这个SO主题,但距离那时已经差不多两年了.在最近的版本中没有什么新东西?

你知道一些解决方法吗?

Jac*_*ekM 35

还有另一个主题涉及此问题:测试是否使用Selenium Webdriver重点关注元素

基本上代码将是

element.equals(driver.switchTo().activeElement());
Run Code Online (Sandbox Code Playgroud)


Bar*_*cki 6

对于python开发人员:

def is_element_focus(id):
    return self.driver.find_element_by_id(id) == self.driver.switch_to.active_element
Run Code Online (Sandbox Code Playgroud)