3

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.

Deepak Yadav
  • 321
  • 1
  • 8
  • 19

5 Answers5

7

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

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • 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. – Deepak Yadav May 11 '18 at 07:26
  • It should work with TAB to go to the next active element. You need to use the activeElement() method again for the new element. – Grasshopper May 11 '18 at 07:41
  • Hi, after fiddling with for some moments because of missing frames. I'm able to use this solution. – Deepak Yadav May 17 '18 at 12:39
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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ritwik Gupta
  • 115
  • 6
1

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");
    }
NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • 1
    Don't you think these many explicit wait (worst case of explicit wait ) would be bad practice ? – cruisepandey May 11 '18 at 06:43
  • @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 – NarendraR May 11 '18 at 06:45
  • 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(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 May 11 '18 at 07:22
0

if u want to use tab button to particular locator then

WebElement inputField = driver.findElement(By.Locator("LocatorValue"));
inputField.sendKeys(Keys.TAB);
bhupathi turaga
  • 297
  • 2
  • 16
0

To navigate to the next component, you can use Class KeyboardFocusManager:

KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();

To Learn more about this use link https://docs.oracle.com/javase/7/docs/api/java/awt/KeyboardFocusManager.html

Sneha Shinde
  • 366
  • 1
  • 7