How to use Sendkeys() without finding element on web page when element is already focused and waiting for input

Deepak Yadav picture Deepak Yadav · May 11, 2018 · Viewed 9.6k times · Source

I'm trying to write a simple Java based selenium code where I would load a page, give the desired values to username & password and login to web page.

Now once the web page loads it automatically waits for user to enter username i.e username is already focused. So can I send the keys to this already focused element. Once I have given the input to username I could use TAB to select the next input i.e. password and then TAB again to select the Login button.

Answer

Grasshopper picture Grasshopper · May 11, 2018

Try this - WebElement currentElement = driver.switchTo().activeElement();

Refer to this for more details - https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html#activeElement--