'How do I get the currently focused element in HtmlUnit

In my javascript I did

document.getElementById(myID).focus();

to focus the element.

Rather than trying to traverse through the DOM tree, I was wondering if I can simply say something like

var element = (HtmlTextInput) webClient.getActieWindow().getFocusedElement();

but I can't seem to find it in the API



Solution 1:[1]

I needed the page as well to get the element

HtmlTextInput textInput =
 (HtmlTextInput) ((HtmlPage) webClient
    .getCurrentWindow()
    .getEnclosedPage())
  .getFocusedElement();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Archimedes Trajano