Different operations or commands used on the WebElement


Let’s start with the basic question. What is WebElement?

In a simple language WebElement is “Value” enclosed in  HTML tags.

Eg: for <p> HTML paragraph. </p> The webelement is everything within <P>… </P>.

In Selenium We are using “findElement” or “findElements” command to find WebElement and perform an action on them.

Below is the syntax:

WebElement element = driver.findElement(By.id(“Username“));

This WebElement can be of any type like it can be a Text, Link, Radio Button, Drop Down, WebTable or any HTML element. But all the actions will always populate against any element irrespective of whether the action is valid for that particular element or not.

For Eg: SendKeys command will be populated even we are performing an action on”link”.

So let’s see most commonly used commands.

Clear Command:

This command will clear the text box value. This accepts nothing and returns nothing.

Syntax :

element.clear();

 

Here is a sample:

WebElement element = driver.findElement(By.id("nameOfElement"));

element.clear();

//Or can be written as

driver.findElement(By.id("nameOfElement")).clear();

SendKeys Command:

This method types the values into “input-box” it accepts Char Sequence as a parameter and returns nothing.

Syntax :

element.sendKeys(“text”);

Here is a sample :

WebElement element = driver.findElement(By.id("Userid"));

element.sendKeys("Hemant");

//Or can be written as

driver.findElement(By.id("Userid ")).sendKeys("hemant");

 

Click Command:

This command performs the simulation of clicking on the WebElement.

Syntax:

element.click();

click() is used to simulate the click action on text elements, links, radio boxes.

Below is a sample:

WebElement element = driver.findElement(By.linkText("Hemant"));

element.click();

//Or can be written as

driver.findElement(By.linkText("Hemant")).click();

IsDisplayed Command:

This command checks whether the element is visible or not and returns “True”/ “False” in response.

Syntax: element.isDisplayed();

Here is a sample:

WebElement element = driver.findElement(By.id("Usernme"));

boolean status = element.isDisplayed();

//Or can be written as

boolean staus = driver.findElement(By.id("Usernme")).isDisplayed();

 

IsEnabled Command:

This command checks whether the element is currently “Enabled or Not”.

Syntax:   

element.isEnabled();

Below is the sample code:

WebElement element = driver.findElement(By.id("UserName"));

boolean status = element.isEnabled();

//Or can be written as

boolean staus = driver.findElement(By.id("UserName")).isEnabled();

//Or can be used as

WebElement element = driver.findElement(By.id("userName"));

boolean status = element.isEnabled();

// Check that if the Text field is enabled, if yes enter value

if(status){

element.sendKeys("Hemant");

}

Submit Command:
This method performs the same operation as that of click method. It specifically needed to be used if the current element is enclosed in the “Form”.

Syntax:

element.submit();

Below is the sample:

WebElement element = driver.findElement(By.id("SubmitButton"));

element.submit();

//Or can be written as

driver.findElement(By.id("SubmitButton")).submit();

 


Leave a Reply