HOME
WebElement represents HTML DOM(Document Object Model) element. Anything present on web page like alert box, text box, button- all are WebElements. When HTML loads into a web browser, it is convert into document object
WebElement in Selenium is a generic class that converts object into document object. There are a number of WebElement commands present in Selenium, but in this blog will discuss about
1) sendKeys()
2) clear()
3) Submit()
4) click()
1) SendKeys – This simulate typing into an element, which may set its value. This method accepts CharSequence as a parameter and returns nothing.
This method works fine with text entry elements like INPUT and TEXTAREA elements.
Input boxes refer to either of these two types:
1. Text Fields– text boxes that accept typed values and show them as they are.
2. Password Fields– text boxes that accept typed values but mask them as a series of special characters (commonly dots and asterisks) to avoid sensitive values to be displayed.
To enter text into the Text Fields and Password Fields, sendKeys() is the method available on the WebElement.
Syntax:
sendKeys(CharSequence… keysToSend ) : void
Command:
driver.findElement(By.xpath("//*[@name = 'email']")).sendKeys("abc123@gmail.com")
2) Clear – If this element is a text entry element, this will clear the value. This command does not require any parameter and return nothing.
Syntax:
clear( ) : void
Command:
email.clear();
3) Submit – This method works well/better than the click(), if the current element is a form, or an element within a form. This command does not require any parameter and return nothing.
Syntax:
submit():void
Command:
driver.findElement(By.id("u_0_2")).submit();
4) Click – This simulates the clicking of any element. This command does not require any parameter and return nothing.
The buttons can be accessed using the click() method.
Syntax:
click():void
Command:
driver.findElement(By.id("u_0_2")).click();
Let us write a small program to see the use of these commands. I have used both click() and submit commands() to show how they work, but when you try to run this program, comment any one of the part.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Facebook_Login {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
WebElement email = driver.findElement(By.xpath("//*[@name = 'email']"));
WebElement password = driver.findElement(By.xpath("//*[@name = 'pass']"));
Thread.sleep(1000);
// switch from main window to child window
for (String handle1 : driver.getWindowHandles()) {
driver.switchTo().window(handle1);
}
driver.findElement(By.id("u_0_k")).click();
// Enter data in Text box
email.sendKeys("abc123@gmail.com");
password.sendKeys("Abc123@");
System.out.println("Data is entered in Text Field");
// Delete values from text box
email.clear();
password.clear();
System.out.println("Data is cleared from Text Field");
// Using submit method to submit the form
email.sendKeys("abc123@gmail.com");
password.sendKeys("Abc123@");
driver.findElement(By.id("u_0_2")).submit();
System.out.println("Login Done with Submit");
}
}
Output:
Data is entered in Text Field
Data is cleared from Text Field
Login Done with Submit
