Web-Elements-Commands-in-Selenium

WebElement commands Cheat Sheet

In this blog we would learn what are the different Web Element commands and how they are used in Automation suite. We will also present a cheat sheet summarising all the important WebElements commands.

But first let us understand what Web Element is?

A Web Element refers to a HTML Element through which Selenium can interact with various commands and methods.

The HTML element consists of start tag and end tag. In between those tags it contains content.

e.g.:

 <div id=”Selenium”>

<class>

</class>

</div>

List of commands which can be used to interact with Web Element.

Web-Elements-Commands-in-Selenium

So how do you use these commands. You need to have the web driver instance and then use findElement() method.

driver.findElement(By.id(“text”));

User can change the locator as per the convenience.

Now let us go through the different commands and methods which Selenium uses to interact with an HTML page using WebElement

Clear Command

clear() : void – This method is used to clear text in text box, drop down etc. The return type of this method is void and it accepts nothing as parameter.

Syntax – element.clear();

How to use the above method in script?

driver.findElement(By.id(“text”)).clear();

OR

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

element.clear();

The above code will clear the text from field having id=”text”.

Click Command

click() : void – This method is used to click button, link text, partial link text etc. The return type of this method is void and it accepts nothing as parameter.

Syntax – element.click();

How to use the above method in script?

driver.findElement(By.id(“loginbtn”)).click();

OR

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

element.click();

The above code will click on login button having id= loginbtn

driver.findElement(By.linkText(“TechCanvass”)).click();

OR

WebElement element = driver.findElement(By.linkText(“TechCanvass”));

element.click();

The above code will click on linktext having linkText= TechCanvass.

GetAttributes command

getAttribute(String  args0) : String – The method is used to get the attribute value from a text box.  The return type is String and accepts String as a parameter.

Syntax – element.getAttribute(“text”);

How to use the above method in script?

String empid= driver.findElement(By.id(“empid”)).getAttribute(“text”);

OR

WebElement empid= driver.findElement(By.id(“empid”));

empid.getAttribute(“text”);

The above code will get the attribute value of a field having id=empid.

GetText Command

getText() : void – This method is used to get the inner field value of a text box. The method returns blank output if the field has no text value. The return type is void and it accepts nothing as parameter.

Syntax – element.getText();

How to use the above method in script?

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

element.getText();

OR

driver.findElement(By.id(“textbox”)).getText();

Now let us learn how to use getText() method through an example:

eg. <input name=”TxtSearch” type=”text” id=”TxtSearch” placeholder=”Search Courses here…”>

The output of the above HTML code by using getText() method would be any text written inside text box.

IsDisplayed Command

idDisplayed() : boolean – This method is used to verify the visibility/presence of a web element. If the element is displayed it returns true and if the element is not displayed or hidden it returns NoSuchExlementFound exception. The return type is boolean and it accepts nothing as parameter.

Syntax – element.isDisplayed();

How to use the above method in script?

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

boolean search_button_displayed=element.isDisplayed();

OR

driver.findElement(By.id(“searchbutton”)).isDisplayed();

Now let us learn how to use isDisplayed() method through an example:

if(driver.findElement(By.id(“searchbutton”)).isDisplayed())

                {

                                driver.findElement(By.id(“searchbutton”)).click();

                }

The above code will click button having id ‘searchbutton’ and if it is displayed, then isDisplayed() method will return true and will get inside the ‘if condition’.

IsEnabled Command

isEnabled() : boolean – This method is used to verify the state of the web element. Basically it checks whether the element is clickable or not. If the element is enabled it returns true. The return type is boolean and it accepts nothing as parameter.

Syntax – element. isEnabled();

How to use the above method in script?

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

boolean search_button_displayed=element.isEnabled();

OR

driver.findElement(By.id(“searchbutton”)).isEnabled();

Now let us learn how to use isEnabled() method through an example:

if(driver.findElement(By.id(“searchbutton”)).isEnabled())

                {

                                driver.findElement(By.id(“searchbutton”)).click();

                }

The above code will verify if the web element having id ‘searchbutton’ is enabled, if true then it will get inside the ‘if condition’ and click the search button.

IsSelected Command

isSelected() : boolean – This method verifies if the web element is selected or not. It is usually used with dropdowns, checkbox’s and radio buttons. The return type is boolean and it accepts nothing as parameter.

Syntax – element.isSelected();

How to use the above method in script?

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

boolean search_button_enabled=element.isEnabled();

OR

driver.findElement(By.id(“searchbutton”)).isEnabled();

Now let us learn how to use isSelected() method through an example:

if(driver.findElement(By.id(“male_gender”)).isSelected());

                {

                                driver.findElement(By.id(“register”)).click();

                }

The above code will verify if the web element having id ‘male_gender’ radio button is selected, if true then it will get inside the ‘if condition’ and click the register button.

GetTagName Command

getTagName( ) : String – This method gets the tag name of the web element. The return type is String and it accepts nothing as parameter.

Syntax – element.getTagName( );

How to use the above method in script?

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

element.getTagName();

OR

String tagname= driver.findElement(By.id(“loginbtn”)).getTagName();

Now let us learn how to use getTagName() method through an example:

<input type=”submit” name=”loginbtn” >

The output of the above web element would be ‘input’ if we use ‘getTagName()’ method.

Now let us see how different Web Element methods can be used in a single script.

package TechC;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class WebElements {

                public static void main(String args []) throws InterruptedException

                 {            

                                System.setProperty(“webdriver.chrome.driver”, “<WebDriver Path>”);  

                                WebDriver driver = new ChromeDriver();

                                driver.get(“http://techcanvass.com/”);

                                driver.findElement(By.id(“TxtSearch”)).click();

                                String text=driver.findElement(By.xpath(“//div[@class=’intro-content’]//h1[1]”)).getText();

                                System.out.println(“Text is: “+text);

                                driver.findElement(By.id(“TxtSearch”)).sendKeys(“Selenium”);

                                if(driver.findElement(By.id(“BtnSearch”)).isDisplayed())

                                {

                                                String tagname=driver.findElement(By.id(“BtnSearch”)).getTagName();

                                                System.out.println(“TagName is: “+tagname);

                                                driver.findElement(By.id(“BtnSearch”)).click();

                                }

                  }

}

We have used TechCanvass.com website to demonstrate different Web Element methods in the above code.

About Techcanvass

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

You can join our Selenium Training in Mumbai (Classroom training), Live Online Selenium Training or can even go for Selenium Self-learning course. Each of these courses are offered through TechEdge LMS portal.