Sample examples for sendKeys(),click() and clear() WebElement methods in selenium WebDriver
1. sendKeys() webelement method with example
sendKeys(java.lang.charSequence... keysToSend)
sets value in the elements.
Example :
package com.selenium.practise;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestGoogle {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Soft Wares\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Today's Weather");//Set the value in the element
driver.close();
}
}
(or)
package com.selenium.practise;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestGoogle {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Soft Wares\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
WebElement input=driver.findElement(By.name("q"));
input.sendKeys("Today's Weather"); //Set the value in the element
driver.close();
}
}
1. clear() webelement method with example
clear()
: This will clear the value if this element is a text entry element.
Example :
package com.selenium.practise;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestGoogle {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Soft Wares\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Today's Weather");
driver.findElement(By.name("q")).clear();//clearing the value
driver.findElement(By.name("q")).sendKeys("WebElement");
driver.close();
//close the window
}
}
(or)
package com.selenium.practise;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestGoogle {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Soft Wares\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
WebElement input=driver.findElement(By.name("q"));
input.sendKeys("Today's Weather");
input.clear();//clearing the value
input.sendKeys("WebElement");
driver.close();
//close the window
}
}
3. click() webelement method with example
Click()
: It clicks the current element.
Example :
package com.selenium.practise;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestGoogle {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Soft Wares\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Today's Weather");
driver.findElement(By.name("btnK")).click();
}}