Home >>Selenium Tutorial >Selenium WebDriver - WebElement Commands

Selenium WebDriver - WebElement Commands

Selenium WebDriver - WebElement Commands

First we will learn the specific terms relevant to Web elements in WebDriver before proceeding with this section.

What is Web Element?

The word element Network applies to an entity HTML. HTML documents are made of components that are HTML. This consists of a start tag, an end tag and the in-between content. For eg, it writes an HTML element as: "<tagname> content </tagname>"

In WebDriver we have many commands and behavior widely used by web elements. The following screenshot shows the Command Panel for the eclipse web feature.

WebElement element = driver.findElement(By.id("UserName"));  

Here the UserName is the I d attribute value, used as a unique identifier for the desired web element.

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

1. Clear Command

Method:

clear() : void  

Command:

element.clear();  

Code 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:

sendKeys(CharSequence? KeysToSend) : void  

Command:

element.sendKeys("text");

Code snippet:

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

3. Click Command

Method:

click() : void  

Command:

element.click();  

Code snippet:

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

4. IsDisplayed Command

Method:

isDisplayed() : boolean  

Command:

element.isDisplayed();  

Code 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:

isEnabled() : boolean  

Command:

element.isEnabled();  

Code 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("phptpoint ");  
}  

6. IsSelected Command

Method:

isSelected() : boolean  

Command:

element.isSelected();  

Code 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:

submit() : void  

Command:

element.submit(); 

Code 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:

getText() : String  

Command:

element.getText();  

Code snippet:

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

9. GetTagName Command

Method:

getTagName() : String  

Command:

element.getTagName();  

Code 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:

getCssvalue() : String  

Command:

element.getCssValue();  

11. getAttribute Command

Method:

getAttribute(String Name) : String  

Command:

element.getAttribute();  

Code snippet:

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

12. getSize Command

Method:

getSize() : Dimension  

Command:

element.getSize();  

Code 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:

getLocation() : Point  

Command:

element.getLocation();  

Code snippet:

WebElement element = driver.findElement(By.id("SubmitButton"));  
Point point = element.getLocation();  
System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);  

No Sidebar ads