How to use Sendkeys() without finding element on web page when element is already focused and waiting for input
Solution 1
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--
Solution 2
Alternatively you can use the ROBOT function as a workaround. For example: to send 123456 you may use
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_2);
robot.keyRelease(KeyEvent.VK_2);
robot.keyPress(KeyEvent.VK_3);
robot.keyRelease(KeyEvent.VK_3);
robot.keyPress(KeyEvent.VK_4);
robot.keyRelease(KeyEvent.VK_4);
robot.keyPress(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_5);
robot.keyPress(KeyEvent.VK_6);
robot.keyRelease(KeyEvent.VK_6);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
I have also used the Sikuli sendkeys feature in the past succesfully.
Solution 3
Use below code for the same :
driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
I have tried the demo and its fine
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
Thread.sleep(1000);
driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
Thread.sleep(1000);
driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
Thread.sleep(1000);
driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
Thread.sleep(1000);
driver.findElement(By.tagName("body")).sendKeys(Keys.TAB);
System.out.println("OK");
}
Deepak Yadav
Student of engineering who loves to code and explore computing world. I love to program swim and run.I also love military engineering a lot, specially military aviation.
Updated on June 07, 2022Comments
-
Deepak Yadav almost 2 years
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.
-
cruisepandey about 6 yearsDon't you think these many explicit wait (worst case of explicit wait ) would be bad practice ?
-
NarendraR about 6 years@cruisepandey, yes i know its just for pause to see how tab moving element to element. else hard code wait not recommended in automation script
-
Deepak Yadav about 6 yearsHi @NarendraR, I tried to run this code, but i'm getting exception -
*** Element info: {Using=tag name, value=body} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
-
Deepak Yadav about 6 yearsI'm able to see that element is active and on selecting the Keys.TAB it focuses on next object, but when I try to sendKeys to this element, it doesn't work. Also could you please suggest should I just use Key.TAB to move to next element or is there a method to move to next active Element.
-
Grasshopper about 6 yearsIt should work with TAB to go to the next active element. You need to use the activeElement() method again for the new element.
-
Deepak Yadav about 6 yearsHi, after fiddling with for some moments because of missing frames. I'm able to use this solution.