Selenium sendkeys() method to send text to input

sendKeys() method in selenium 

sendKeys() in selenium is also one of the most used method. It is being used to type texts in textbox web element on web page. Before using sendKeys() method, you must have to locate that textbox element using any of the different available element locator strategy like name, id, xpath etc.

Selenium sendkeys method

  • sendKeys() method is part of WebElement interface which extends SearchContext and TakesScreenshot interfaces.
  • sendKeys() method is used to type text in input box.
  • sendKeys() method will not clear existing text in textbox but it will append text.
  • You can type password as well using sendKeys() method.
  • Also you can select value from drop down using sendKeys() method.
Let us see how sendKeys() method is used to type text in input element.

Example of sendKeys() method in selenium

Below given example will sow you how to use sendKeys() method to type text in input element and selecting value from dropdown.
  • Syntax to use sendKeys() method in selenium : driver.findElement(By.element_locator).sendKeys("text to type");

Selenium sendKeys()method example

package testPackage;

import org.openqa.selenium.By;

public class SendkeysExample {

	public static void main(String[] args) {
		System.setProperty("", "D:\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		//Find textbox element by id and type text in it.
		driver.findElement("text1")).sendKeys("First Name");
		//Find dropdown element by id and type text it to select value from drop down.
In above given selenium sendKeys() method example, First sendKeys() method will type text First Name in textbox which is located by id element locator. Second sendKeys() method will type text in drop down web element to select value Renault from it.

No comments:

Post a Comment