1
votes

I am using the sendKeys(key,Keys.TAB) method to navigate through a form.

Actions action = new Actions(driver);

    CharSequence key = null;
    for(int i=0;i<42;i++)
    {
    action.sendKeys(key,Keys.TAB).build().perform();
    }

At the end of every action(a tab key press) I want to know which form element is selected

I want to reach to the 42nd element of the form and cross check whether its the desired element and for that i need to retrieve some of its information. I am new to selenium and I am not able to figure out a way to achieve this.

1

1 Answers

3
votes

You can use WebDriver's TargetLocator class for this purpose.

WebElement currentElement = driver.switchTo().activeElement();

This will return you with current element it focussed currently. If no element is focussed it will return you the body element, which is the case when u launch your browser. Internally it will be returning u the element returned by document.activeElement. So to verify u can always run as:

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement currentElement = (WebElement) js.executeScript("return document.activeElement");