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

11,061

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");
    }
Share:
11,061
Deepak Yadav
Author by

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, 2022

Comments

  • Deepak Yadav
    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
    cruisepandey about 6 years
    Don't you think these many explicit wait (worst case of explicit wait ) would be bad practice ?
  • NarendraR
    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
    Deepak Yadav about 6 years
    Hi @NarendraR, I tried to run this code, but i'm getting exception - *** Element info: {Using=tag name, value=body} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nativ‌​e Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknow‌​n Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Un‌​known Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(Erro‌​rHandler.java:214) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFaile‌​d(ErrorHandler.java:‌​166)
  • Deepak Yadav
    Deepak Yadav about 6 years
    I'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
    Grasshopper about 6 years
    It 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
    Deepak Yadav about 6 years
    Hi, after fiddling with for some moments because of missing frames. I'm able to use this solution.