Basic WebElement commands

In this blogpost, we are going to have a brief understanding of basic web element commands. These commands will be applicable to almost all the DOM elements on the web page.

click command :

Usage : Used to click the particular web element.

Syntax : void click();

Return type : void (Nothing)

Parameters : Nothing

Applicability : Link, button, text field, radio button, checkbox

Example :

WebDriver driver = new FirefoxDriver();
driver.get(“https://seleniumatfingertips.wordpress.com”);
driver.manage().window().maximize();

WebElement homeLink = driver.findElement(By.linkText(“Home”));

homeLink.click();


sendKeys command :

Usage : Used to simulate typing into a web element.

Syntax :   void sendKeys(String str);

Return type : void (Nothing)

Parameters : String

Applicability : Text field, Text area

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

homeLink.sendKeys(“selenium”);


clear command :

Usage : Used to clear the values of text entry fields like text field and text area

Syntax :   void clear()

Return type : void (Nothing)

Parameters : Nothing

Applicability : Text field, Text area

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

homeLink.sendKeys(“selenium”);

homeLink.clear();


findElement command :

Usage : Used to find the element using any of the locating mechanism.

Syntax :   WebElement findElement(By by)

Return type : WebElement

Parameters : By reference

Applicability : Any DOM element

Example : 

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

WebElement contentElement = containerElement.findElement(By.tagName(“h1”));


findElements command :

Usage : Used to find the elements using any of the locating mechanism

Syntax :   List<WebElement> findElements(By by)

Return type : List<WebElement> (a list of WebElements)

Parameters : By reference

Applicability : Any DOM element

Example :

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

List<WebElement> records = tableElement.findElements(By.tagName(“tr”));

System.out.println(“Number of records : ” + records.size());

Both, findElement and findElements commands are used to locate the child element (inner element) of the particular web element.


getText command :

Usage : Used to get the visible text of the web element.

Syntax :   String getText()

Return type : String (The inner text of the web element, including sub-elements)

Parameters : Nothing

Applicability : Any DOM element

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

String innerText = homeLink.getText();

System.out.println(innerText);


getTagName command :

Usage : Used to get the tag name of the web element

Syntax :   String getTagName();

Return type : String (The tag name of element)

Parameters : Nothing

Applicability : Any DOM element

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

String tagName = homeLink.getTagName();

System.out.println(tagName);


getAttribute command :

Usage : Used to get the value of any HTML attribute of the web element

Syntax :   String getAttribute(String attributeName);

Return type : String (The value of  passed attributeName for the element)

Parameters : String (HTML Attribute name like id, name, href etc.)

Applicability : Any DOM element

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

String idValue = homeLink.getAttribute(“id”);

System.out.println(idValue);


getCssValue command :

Usage : Used to get the value of any CSS property of the web element.

Syntax :   String getCssValue(String cssPropertyName);

Return type : String (The value of  passed CSS property for the element)

Parameters : String (CSS property name like font-size, color etc.)

Applicability : Any DOM element

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

String fontSize = homeLink.getCssValue(“font-size”);

System.out.println(fontSize);


getLocation command :

Usage : Used to get the location of the web element.

Syntax :   Point getLocation();

Return type : Point class instance

Parameters : Nothing

Applicability : Any DOM element

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

Point p = homeLink.getLocation();
System.out.println(“xOffset : ” + p.x);
System.out.println(“yOffset : ” + p.y);


getSize command :

Usage : Used to get the size of the web element.

Syntax :   Dimension getSize();

Return type : Dimension class instance

Parameters : Nothing

Applicability : Any DOM element

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

Dimension size = homeLink.getSize();
System.out.println(“Width : ” + size.width);
System.out.println(“Height : ” + size.height);


isDisplayed command :

Usage : Used to check whether the element is displayed or not.

Syntax :   boolean isDisplayed()

Return type : boolean

Parameters : Nothing

Applicability : Any DOM element

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

System.out.println(“Home link is displayed : ” + homeLink.isDisplayed());


isEnabled command :

Usage : Used to check whether is enabled or not.

Syntax :   boolean isEnabled();

Return type : boolean (returns false for disabled elements)

Parameters : Nothing

Applicability : Any DOM element

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

System.out.println(“Home link is enabled : ” + homeLink.isEnabled());


isSelected command :

Usage : Used to check whether the element is selected/ checked or not.

Syntax :   boolean isSelected()

Return type : boolean

Parameters : Nothing

Applicability : Radio button, Checkbox, Dropdown menu

Example :

WebElement homeLink = driver.findElement(By.linkText(“Home”));

System.out.println(homeLink.isSelected());

That’s all from the post.

Hope it helps you!!

Cheers 🙂

Advertisements

#clear, #click, #get-css-attribute, #get-html-attribute-value, #get-location-of-element, #get-size-of-element, #isdisplayed, #isenabled, #isselected, #sendkeys, #webelement-commands