Selenium:检查WebElement是否具有焦点

时间:2013-06-10 13:38:57

标签: selenium selenium-webdriver

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

:focus 

伪类。

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

我知道this SO topic,但距离那时已近两年了。在最近的版本中没有什么新东西?

你知道一些解决方法吗?

2 个答案:

答案 0 :(得分:34)

还有另一个主题涉及此问题:Test if an element is focused using Selenium Webdriver

基本上代码是

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

答案 1 :(得分:6)

对于python开发人员:

def is_element_focus(id):
    return self.driver.find_element_by_id(id) == self.driver.switch_to.active_element