WebElement Actions

In this selenium tutorial , we will learn all the different user actions that can be performed on an WebElement.

Different WebElements will have different actions that can be taken on them. For example, in a textbar element, we can type in some text or clear the text that is already typed in it.

Similarly for a button, we can click on it, get the dimensions of it, and position of it on the webpage, but we cannot type into a button, and for a link, we cannot type into it.

All the actions are present as method in WebElement interface in selenium webdriver.

In case we try to execute a wrong action on a WebElement, we don’t see any exception or error thrown and also we don’t see any action that really gets executed; WebDriver ignores such actions.

The getAttribute() method

The getAttribute() action can be executed on all the WebElements present in the webpage. They are generally key-value pairs appearing in the start tag of an element. For example, Consider  following WebElement:

<label name=”username” id=”uname”>Enter Username: </label>

Here name and id are the attributes or attribute keys and Username and uname are the attribute values.

Syntax:

java.lang.String getAttribute(java.lang.String name)

Example:

Consider Google Search Button;

<button id=”gbqfba” class=”gbqfba” name=”btnK” aria-label=”Google Search”>

Script:

public class GetAttrib {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement sb= driver.findElement(By.name("btnK"));

		System.out.println("Name of the button is: " +sb.getAttribute("name"));

		System.out.println("Id of the button is: " +sb.getAttribute("id"));

		System.out.println("Class of the button is: " +sb.getAttribute("class"));
	}
}

In the above code getAttribute() method is to fetch the attribute values of the attribute name, id, class, and aria-label of the Google Search button WebElement.

 

OutPut:

Name of the button is: btnK
Id of the button is: gbqfba
Class of the button is: gbqfba

 

The sendKeys() method:


This is used to type text into the textbox, sendKeys() action is applicable for textbox or textarea HTML elements as well for buttons

Syntax

void sendKeys(java.lang.CharSequence…keysToSend)

The input parameter for the preceding method is CharSequence of text that has to be entered into the element. This method doesn’t return anything.

Example

Type a search text into the Google Search box using the sendKeys() method.

public class sendKeysExample {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchBox = driver.findElement(By.name("q"));
		searchBox.sendKeys("java selenium world");
	}
}

If you want to type in some special keys, such as Backspace, Enter, Tab, Shift etc. then you need to use a special enum class of WebDriver named Keys .

Example:

public class SendKeysSecondExample {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchBox = driver.findElement(By.name("q"));
		searchBox.sendKeys(Keys.chord(Keys.SHIFT,"java selenium world"));
	}
}

In the above code, the chord() method from the Keys enum is used to type the key while the text specified is being given as an input to be the textbox. i.e Shift Key + “Text”

So in the above program all the text being typed in uppercase.

 

The clear() method

The clear() method is similar to the sendKeys() method, which is applicable for textbox and textarea elements.

This is used to erase the text that is entered in a WebElement using the sendKeys() method.

Syntax:

void clear();

This method doesn’t take any input and doesn’t return any output. It is simply executed on the target text entry element.

Example:

public class ClearExample{
	public static void main(String[] args){
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchBox = driver.findElement(By.name("q"));
		searchBox.sendKeys(Keys.chord(Keys.SHIFT,"java selenium world"));
		searchBox.clear();
	}
}

WebElement’s clear() method to clear the text after typing java selenium worldinto the Google Search box.

This can be achieved using the Keys.BACK_SPACE enum also.

 

The submit() method:

The submit method can be taken on a form or on an element, which is inside a form. This is used to submit a form of a web page to the server hosting the web application.

Syntax:

void submit();

The preceding method doesn’t take any input parameter and doesn’t return anything. But a NoSuchElementException is thrown when this method is executed on a WebElement that is not present within a form.

Example:

public class SubmitExample {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchBox = driver.findElement(By.name("q"));
		searchBox.sendKeys(Keys.chord(Keys.SHIFT,"java selenium world"));
		searchBox.submit();
	}
}

In the above code search box is not a part of any form. So you should encounter a  NoSuchElementException message.

So when you use the submit() method on a WebElement, make sure it is part of the form element.

 

The getCssValue() method

The getCssValue action can be taken on all the WebElements.

This is used to fetch the CSS properties’ values of the given element,  CSS properties can be font-family, background-color, color, and so on.

Syntax:

java.lang.String getCssValue(java.lang.String propertyName)

Example:

public class GetCSSValueSample {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchButton = driver.findElement(By.name("btnK"));
		System.out.println(searchButton.getCssValue("font-family"));
	}
}

The getCssValue() method to find the font-family of the text visible on the Google Search button.

Similarly, we can retrieve the background color of an element using this method.

public class GetBackGroungColor {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchButton = driver.findElement(By.name("btnK"));
		System.out.println(searchButton.getCssValue("background-color"));
	}
}

Output:

Transparent

 

The getLocation() method

The getLocation method can be executed on all the WebElements,  getLocation to get the position of an element where it is rendered on the web page.

This position is calculated relative to the top-left corner of the web page ( not the browser )of which the (x, y) coordinates are assumed as (0, 0).

This method will be of use if your test script tries to validate the layout of your web page.

Syntax

Point getLocation();

Example:

public class GetLocationOfSearchButton {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchButton = driver.findElement(By.name("btnK"));
		System.out.println(searchButton.getLocation());
	}
}

Output:

(470, 450)

 

The getSize() method

The getSize method can also be applied on all the visible components of HTML. getSize() will return the width and height of the rendered WebElement. Return type of the getSize() method is Dimention class

Dimension getSize()

This method doesn’t take any input parameters, and the return type is a class instance named Dimension. This class contains the width and height of the target WebElement.

Example:

public class GetSizeOfGoogleSearchButton {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchButton = driver.findElement(By.name("btnK"));
		System.out.println(searchButton.getSize());
	}
}

Output: (width, height)

(103, 35)

 

The getText() method

The getText() method can be taken on all the WebElements, getText()will give the visible text if the element contains any text on it or else will return nothing.

Syntax:

java.lang.String getText();

Example:

public class GetTextExample {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchButton = driver.findElement(By.name("btnK"));
		System.out.println(searchButton.getText());
	}
}

Output:

Google Search

 

The getTagName() method

The getTagName action can be taken on all the WebElements.  getTagName() will return the tag name of the WebElement.

Syntax:

java.lang.String getTagName()

Example:

<button id=”gbqfba” class=”gbqfba” name=”btnK” aria-label=”Google Search”>

Script:

public class GetTagNameExample {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchButton = driver.findElement(By.name("btnK"));
		System.out.println(searchButton.getTagName());
	}
}

Output:

Button

 

The isDisplayed() method

The isDisplayed action verifies if an element is displayed on the web page and can be executed on all the WebElements.

Syntax:

boolean isDisplayed();

Example:

public class isDisplayedExample {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchButton = driver.findElement(By.name("btnK"));
		System.out.println(searchButton.isDisplayed());
	}
}

The isEnabled() method

The isEnabled action verifies if an element is enabled on the web page and can be executed on all the WebElements.

Syntax:

boolean isEnabled();

Example:

public class isEnabledExample {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchButton = driver.findElement(By.name("btnK"));
		System.out.println(searchButton.isEnabled());
	}
}

The isSelected() method

The isSelected action verifies if an element is selected right now on the web page and can be executed only on a radio button, options in select, and checkbox WebElements.

When executed on other elements, it will return false.

Syntax:

boolean isSelected()

Example:

public class IsSelected {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.google.com");
		WebElement searchBox = driver.findElement(By.name("q"));
		System.out.println(searchBox.isSelected());
	}
}

It returns false for the Google Search box, because this is not a radio button, options in select, or a checkbox.

Leave a Reply

Your email address will not be published. Required fields are marked *