Selenium SendKeys : All You Need To Know

What Is Selenium sendKeys() Method?

package sendkeys;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SendkeysTest {

WebDriver driver;

//Method to set up the browser and open the dummy website
@BeforeClass
public void setUp() {

System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.lambdatest.com");
driver.manage().window().maximize();
}

@Test
public void testSendkeys() {
//Locate the email and use sendkeys to pass the email to the field
driver.findElement(By.id("useremail")).sendKeys("----your email id----");

//Locate the Start Free Testing button and click it
driver.findElement(By.xpath("//*[@id='testing_form']//button")).click();
}


@AfterClass
public void burnDown() {
driver.quit();
}

}
WebElement emailField = driver.findElement(by.id(“useremail”));
emailField.sendKeys(“---your email id---”);

Using Selenium sendKeys To Erase Text

package sendkeys;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SendkeysTest {

WebDriver driver;

//Method to set up the browser and open the dummy website
@BeforeClass
public void setUp() {

System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.lambdatest.com");
driver.manage().window().maximize();
}

@Test
public void testSendkeys() {
//Locate the email and use sendkeys to pass the email to the field
WebElement email = driver.findElement(By.id("useremail"));
email.sendKeys("---your email id---");

// Erase the text entered in the email id field
email.clear();
}


@AfterClass
public void burnDown() {
driver.quit();
}

}

How Can We Type In Selenium Without Using sendKeys?

Using The JavaScriptExecutor

JavascriptExecutor jse = ((JavascriptExecutor)driver);         
WebElement email = driver.findElement(By.id("useremail"));
jse.executeScript("arguments[0].value='---your email id---';", email);

Implementing Selenium sendKeys On LambdaTest Grid

package sendkeys;
import java.awt.AWTException;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SendkeysTest {


public String username = "--- your user name ---";
public String accesskey = "--- your access key ---";
public static RemoteWebDriver driver = null;
public String gridURL = "@hub.lambdatest.com/wd/hub";
boolean status = false;
//Method to set up the browser and open the dummy website
@BeforeClass
public void setUp() {

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("version", "81.0");
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will get any available one
capabilities.setCapability("build", "LambdaTestProject");
capabilities.setCapability("name", "LambdaTestSendKeysProject");
capabilities.setCapability("network", true); // Enables network logs
capabilities.setCapability("visual", true); // Enables step by step screenshot
capabilities.setCapability("video", true); // Enables video recording
capabilities.setCapability("console", true); // Captures console logs
try {
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
driver.get("https://www.lambdatest.com");
}

@Test
public void testSendkeys() throws AWTException {


//Locate the email and use sendkeys to pass the email to the field

WebElement email = driver.findElement(By.id("useremail"));
email.sendKeys("abc@abc.com");


//Locate the Start Free Testing button and click it
driver.findElement(By.xpath("//*[@id='testing_form']//button")).click();


}


@AfterClass
public void burnDown() {
driver.quit();
}

}

Conclusion

--

--

--

I am a Computer Science Engineer by degree, and a Digital Marketer by heart who works at LambdaTest. A social media maven!!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Power BI May 2022 Feature Summary

Hosting your Website using Amplify in AWS

Campmates Mission

Let’s Get the Train Going

AppSeed Oct.2020 — New Products: Datta Able Jinja, Volt Bootstrap 5, Azia Dashboard

Azia Dashboard PRO — Premium Dashboard Template.

Simple Tricks to Make your Cursor Fly on macOS

TLDR for new technologies (for architects that have little time)

Stop Using Dirty Diamonds in Java

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Praveen Mishra

Praveen Mishra

I am a Computer Science Engineer by degree, and a Digital Marketer by heart who works at LambdaTest. A social media maven!!

More from Medium

Create TestNG Project In Eclipse & Run Selenium Test Script

🔁Switching Testng.xml files With Maven Profiling

Cypress VS Selenium

How does Selenium Page Factory work with Cucumber?