Ripon Al Wasim
Ripon Al Wasim

Reputation: 37826

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:

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

Upvotes: 2

Views: 5160

Answers (3)

Ripon Al Wasim
Ripon Al Wasim

Reputation: 37826

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);

Upvotes: 0

Arek
Arek

Reputation: 2031

You could also try the direct webdriver method:

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. This matches the semantics of calling "document.activeElement" in Javascript. Returns: The WebElement with focus, or the body element if no element with focus can be detected.

Upvotes: 2

GitaarLAB
GitaarLAB

Reputation: 14655

You could check document.activeElement
This is supported in all major browsers.

See this on SO for more information. This question is probably a duplicate of this one.

Good luck!

Upvotes: 1

Related Questions