简体   繁体   English

如何验证文本字段上是否存在光标/焦点

[英]How to verify cursor/focus is present on a text field

I have a text field after login. 登录后我有一个文本字段。 After login cursor will be focused on that text field automatically. 登录后,光标将自动聚焦在该文本字段上。 How can I verify whether cursor/focus is present or not on that text field? 如何验证该文本字段中是否存在光标/焦点?

Here is the HTML code of text field: 这是文本字段的HTML代码:

<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">

You could also try the direct webdriver method: 您还可以尝试直接webdriver方法:

driver.switchTo().activeElement()

Switches to the element that currently has focus within the document currently "switched to", or the body element if this cannot be detected. 切换到当前“切换到”文档中当前具有焦点的元素,或者如果无法检测到,则切换到body元素。 This matches the semantics of calling "document.activeElement" in Javascript. 这与Javascript中调用“document.activeElement”的语义相匹配。 Returns : The WebElement with focus, or the body element if no element with focus can be detected. 返回 :具有焦点的WebElement,或者如果无法检测到具有焦点的元素,则返回 body元素。

You could check document.activeElement 您可以检查document.activeElement
This is supported in all major browsers. 所有主流浏览器都支持此功能。

See this on SO for more information. 有关详细信息,请参阅SO This question is probably a duplicate of this one. 这个问题可能与此问题重复。

Good luck! 祝好运!

My details answer is: 我的详细解答是:

WebElement actual = (WebElement)jse.executeScript("return document.getElementById('widget_polarisCommunityInput_113_title');");
WebElement expected = (WebElement)jse.executeScript("return document.activeElement;");
assertEquals(actual, expected);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM