如何在Selenium RC中测试哪个元素具有焦点?

hwi*_*ers 7 selenium-rc

如何在Selenium RC中测试哪个元素具有焦点?

rcr*_*ick 6

当我遇到同样的问题时,出于各种原因,其他答案都没有对我有用.我最终做了以下(使用Java中的Selenium 2.0 WebDriver).

WebDriver driver = new FirefoxDriver();
String elScript = "return document.activeElement;";

// Note that the executeScript call returns a WebElement, 
// so you can do quite a lot with the result.
WebElement focuseedEl  = (WebElement) ((JavascriptExecutor) driver).executeScript(elScript);
Run Code Online (Sandbox Code Playgroud)

该评论澄清了我的困惑点:executeScript根据你告诉它执行的内容返回不同的东西,这可能非常棘手.


小智 2

我无法让:focus伪类想法发挥作用,不知道为什么 - 目标css=#search_input input.text匹配,但css=#search_input input.text:focus没有?但这对我有用:

self.assertEquals(self.se.get_element_index('dom=document.activeElement'),
     self.se.get_element_index('//input[@id="search-query"]'))
Run Code Online (Sandbox Code Playgroud)

这是使用Python Selenium API,因此get_element_index()调用是核心 Selenium 命令列表的包装器。将此适应您的环境。它正在评估文档的焦点元素的元素索引(使用 Javascript DOM 定位器获取)和要测试焦点的元素的元素索引(使用 XPath 定位器获取)。另请参阅此问题