Selenium WebDriver - WebElement Commands

Before proceeding with this section, first we should know the basic terminology related to web elements in WebDriver.

What is Web Element?

The term web element refers to a HTML element. The HTML documents are composed of HTML elements. It consists a start tag, an end tag and the content in between. For instance, a HTML element is written as: "<tagname> content </tagname>"

In WebDriver, we have several commonly used web element commands and actions. The following screenshot displays the eclipse web element command panel.

Selenium WebDriver - WebElement Commands
Note
Note: To get the web element object, we have to write the statement as:
snippet
WebElement element = driver.findElement(By.id("UserName"));

Here, the UserName is the value of the id attribute, used as a unique identification for the desired web element.

Given are some of the most commonly used WebElement commands for Selenium WebDriver.

1. Clear Command

Method:

snippet
clear() : void

Command:

snippet
element.clear();

Code snippet:

snippet
WebElement element = driver.findElement(By.id("UserName"));
element.clear();
             
//Or can be written as
             
driver.findElement(By.id("UserName")).clear();

2. Sendkeys Command

Method:

snippet
sendKeys(CharSequence? KeysToSend) : void

Command:

snippet
element.sendKeys("text");

Code snippet:

snippet
WebElement element = driver.findElement(By.id("UserName"));
element.sendKeys("rookienerd");
             
//Or can be written as
             
driver.findElement(By.id("UserName")).sendKeys("rookienerd");

3. Click Command

Method:

snippet
click() : void

Command:

snippet
element.click();

Code snippet:

snippet
WebElement element = driver.findElement(By.linkText("rookienerd"));
element.click();
             
//Or can be written as
             
driver.findElement(By.linkText("rookienerd")).click();

4. IsDisplayed Command

Method:

snippet
isDisplayed() : boolean

Command:

snippet
element.isDisplayed();

Code snippet:

snippet
WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isDisplayed();
             
//Or can be written as
             
boolean staus = driver.findElement(By.id("UserName")).isDisplayed();

5. IsEnabled Command

Method:

snippet
isEnabled() : boolean

Command:

snippet
element.isEnabled();

Code snippet:

snippet
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("rookienerd");
}

6. IsSelected Command

Method:

snippet
isSelected() : boolean

Command:

snippet
element.isSelected();

Code snippet:

snippet
WebElement element = driver.findElement(By.id("Sex-Male"));
boolean status = element.isSelected();
             
//Or can be written as
             
boolean staus = driver.findElement(By.id("Sex-Male")).isSelected();

7. Submit Command

Method:

snippet
submit() : void

Command:

snippet
element.submit();

Code snippet:

snippet
WebElement element = driver.findElement(By.id("SubmitButton"));
element.submit();
             
//Or can be written as
             
driver.findElement(By.id("SubmitButton")).submit();

8. GetText Command

Method:

snippet
getText() : String

Command:

snippet
element.getText();

Code snippet:

snippet
WebElement element = driver.findElement(By.xpath("anyLink"));
String linkText = element.getText();

9. GetTagName Command

Method:

snippet
getTagName() : String

Command:

snippet
element.getTagName();

Code snippet:

snippet
WebElement element = driver.findElement(By.id("SubmitButton"));
String tagName = element.getTagName();
             
 //Or can be written as
             
 String tagName = driver.findElement(By.id("SubmitButton")).getTagName();

10. getCssValue Command

Method:

snippet
getCssvalue() : String

Command:

snippet
element.getCssValue();

11. getAttribute Command

Method:

snippet
getAttribute(String Name) : String

Command:

snippet
element.getAttribute();

Code snippet:

snippet
WebElement element = driver.findElement(By.id("SubmitButton"));
  String attValue = element.getAttribute("id"); //This will return "SubmitButton"

12. getSize Command

Method:

snippet
getSize() : Dimension

Command:

snippet
element.getSize();

Code snippet:

snippet
WebElement element = driver.findElement(By.id("SubmitButton"));
Dimension dimensions = element.getSize();
System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width);

13. getLocation Command

Method:

snippet
getLocation() : Point

Command:

snippet
element.getLocation();

Code snippet:

snippet
WebElement element = driver.findElement(By.id("SubmitButton"));
Point point = element.getLocation();
System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +