WebElement actions in selenium WebDriver

In this tutorial, we are going to learn various types of user actions on a WebElement.

1. sendKeys() method:

This method is used to send text into textbox or textarea and to upload files or images. It first identifies the textbox/textarea by an attribute name and then enters the text into it.

public class sendKeys{

static WebDriver driver;

@Test

public void enterText(){

System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// selenium command to open the web site
driver.get(“http://www.google.com”);
WebElement googleSearchBar = driver.findElement(By.name(“q”));
googleSearchBar.sendKeys(“selenium tutorials”);

}
}


2. clear() method:

clear method is used to clear/remove the text entered using the sendKeys method in a textbox or textarea.

public class clearText{

static WebDriver driver;


@Test

public void clear(){

System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// selenium command to open the web site
driver.get(“http://www.google.com”);
WebElement googleSearchBar = driver.findElement(By.name(“q”));
googleSearchBar.sendKeys(“selenium tutorials”);

googleSearchBar.clear();

}
}

3. getCssValue() method:

It is used to get the css properties of the HTML web element. Most of the HTML web elements contain css properties like font-size, font-family, font-weight, color,  background-color, and backgroung-image etc. getCssValue() method fetches these css values.

// I’m Feeling Lucky button
WebElement button = driver.findElement(By.name(“btnI”));
System.out.println(“Font size is: “+button.getCssValue(“font-size“));
// Google Search button
WebElement button = driver.findElement(By.name(“btnK”));
System.out.println(“Color is: “+button.getCssValue(“color“));

4. getSize() method:

This method is used to get the width and height of any web element. getSize() method doesn’t take any input parameters.

package com;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class GetSizeMethod {
static WebDriver driver;
@Test
public void getSize() {
// chrome standalone server
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// selenium command to open the URL
driver.get(“http://www.google.com”);
// Google Search button
WebElement button = driver.findElement(By.name(“btnK”));
System.out.println(“Google Search button size is: “+button.getSize());
// Sign in button in top right corner
WebElement signIn = driver.findElement(By.id(“gb_70”));
System.out.println(“signIn button size is: “+signIn.getSize());
}
@AfterTest
public void tearDown() {
driver.close();
}
}

5. getText() method:

It can be applied on any WebElement to fetch the visible text. In case, web element doesn’t contain any text, this method will return nothing.

public class GetText {

static WebDriver driver;

@Test
public void text() {

// chrome standalone server
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// selenium command to open the URL
driver.get(“https://www.amazon.in/”);

WebElement text = driver.findElement(By.id(“nav-your-amazon”));
System.out.println(“Text is: “+text.getText());

}

@AfterTest
public void tearDown() {

driver.close();
}
}


Output:
Text is: Your Amazon.in
PASSED: text

6. getTagName() method:

This method is used to get the tag name of the HTML element.

HTML code: <span class=”nav-line-1>Try</span>

In the above code, we are finding out the tagname for Try link in amazon website. It doesn’t have any id or name attributes, so we are using class name to get the tagname.

@Test
public void tagName() {
// chrome standalone server
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// selenium command to open the URL
driver.get(“https://www.amazon.in/”);
WebElement tag = driver.findElement(By.className(“nav-line-1”));
System.out.println(“TagName is: “+tag.getTagName());
}
Output:TagName is: span
PASSED: tagName

7. isDisplayed() method:

This method is used to verify whether an element is displayed or not in the in the web page. It can be applied to all web elements. This method returns a boolean (true or false) value.

package com;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class IsDisplayed {
static WebDriver driver;
@Test
public void is_displayed() {
// chrome standalone server
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// selenium command to open the URL
driver.get(“https://www.icicibank.com/”);
WebElement link1 = driver.findElement(By.linkText(“Ways to Bank”));
if(link1.isDisplayed()){
System.out.println(“‘Ways to Bank’ link is displayed”);
System.out.println(link1.isDisplayed());
}else{
System.out.println(“‘Ways to Bank’ link is not displayed”);
}}@AfterTest
public void tearDown() {driver.close();
}
}Output:

‘Ways to Bank’ link is displayed
true
PASSED: is_displayed

Tags: selenium webelement actions.