[java] sendKeys() in Selenium web driver

I am new to Selenium. I just want to send keys to a username text box and send a tab key both at a time so that text box can check for availability of username.

Here is the code:

 driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys("UserName");
 driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys(Keys.TAB);

But this one is not working.

This question is related to java selenium selenium-webdriver

The answer is


List<WebElement>itemNames = wd.findElements(By.cssSelector("a strong")); 
System.out.println("No items in Catalog page: " + itemNames.size());
   for (WebElement itemName:itemNames)
    {  
       System.out.println(itemName.getText());
    }

I doubt for Keys.TAB in sendKeys method... if you want to use TAB you need to do something like below:

Actions builder = new Actions(driver);
builder.keyDown(Keys.TAB).perform()

For python selenium,

Importing the library,

from selenium.webdriver.common.keys import Keys

Use this code to press any key you want,

Anyelement.send_keys(Keys.RETURN)

You can find all the key names by searching this selenium.webdriver.common.keys.


Try this one, and then import the package:

import org.openqa.selenium.Keys;

driver.findElement(By.xpath("//*[@id='username']")).sendKeys("username");

driver.findElement(By.xpath("//*[@id='username']")).sendKeys(Keys.TAB);

driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("password");

The simplest solution is Go to Build Path > Configure Build Path > Java Compiler and then select the 'Compiler compliance level:' to the latest one from 1.4 (probably you have this).


Try this code:

WebElement userName = pathfinderdriver.switchTo().activeElement();
userName.sendKeys(Keys.TAB);

Try this, it will surely work:

driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys("UserName" + Keys.TAB);

I believe Selenium now uses Key.TAB instead of Keys.TAB.


Try using Robot class in java for pressing TAB key. Use the below code.

driver.findElement(By.xpath("//label[text()='User Name:']/following::div/input")).sendKeys("UserName");

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);

This is a single line command to send the TAB key;

driver.findElement(By.id("Enter_ID")).sendKeys("\t");

I have found that creating a var to hold the WebElement and the call the sendKeys() works for me.

WebElement speedCurrentCell = driver.findElement(By.id("Speed_current"));
speedCurrentCell.sendKeys("1300");

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to selenium

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser Class has been compiled by a more recent version of the Java Environment How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? element not interactable exception in selenium web automation Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception?

Examples related to selenium-webdriver

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception? Scrolling to element using webdriver? Only local connections are allowed Chrome and Selenium webdriver Check if element is clickable in Selenium Java