Selenium WebDriver WebElement Commands

WebElement Commands :

Given are some of the most used webelement commands.

1. Click Command:

This command is used to click on any webElement.

Command
element.click(); 
CODE SNIPPET
WebElement element = driver.findElement(By.id("SaveButton"));  
element.click();  
               
//Or can be written as  
               
driver.findElement(By.id("SaveButton")).click();  

2. Sendkeys Command:

This command is used to pass the text in input field. It accepts the string parameter.

Command
element.sendKeys("text");
CODE SNIPPET
WebElement element = driver.findElement(By.id("SearchBox"));  
element.sendKeys("BookOfNetwork");  
               
//Or can be written as  
               
driver.findElement(By.id("SearchBox")).sendKeys("BookOfNetwork");

3. Clear Command:

This command is used to clear the text in input field.

Command
element.clear(); 
CODE SNIPPET
WebElement element = driver.findElement(By.id("Password"));  
element.clear();  
               
//Or can be written as  
               
driver.findElement(By.id("Password")).clear();  

4. Submit Command:

This is quite similat to click command. The submit command is applicable only for (form) and makes handling of form easier. It can be used with any element inside a form.

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();  

5. GetText Command:

This command is used to get the visible text of that webElement. It returns String value.

Command
element.getText(); 
CODE SNIPPET
WebElement element = driver.findElement(By.id("signupModalButton"));
String text = element.getText();  

6. GetAttribute Command:

This command is used to get the attribute value. It accepts String as parameter and returns String value.

Command
element.getAttribute("attributeName");
CODE SNIPPET
WebElement element = driver.findElement(By.id("Password"));  
  String attValue = element.getAttribute("id"); //This will return "Password"  
  

7. GetLocation Command:

This is used to get the position of that webelement. It returns 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);  

8. GetTagName Command:

This command is used to get the tagname. It returns String value.

Command
element.getTagName();
CODE SNIPPET
WebElement element = driver.findElement(By.id("SaveButton"));  
String tagName = element.getTagName();  
               
 //Or can be written as  
               
 String tagName = driver.findElement(By.id("SaveButton")).getTagName();
 

9. GetSize Command:

This is used to get the size of that element. It returns dimensions.

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);

10. IsSelected Command:

This command is mostly used for radiobutton and checkbox, to check it is selected or not. It accepts nothing as parameter and returns a boolean.

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

11. IsEnabled Command:

This command is used to check that element is active or not. It accepts nothing as parameter and returns a 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();  
  

12. IsDisplayed Command:

This command is used to check the visibility of element. It accepts nothing as parameter and returns a boolean.

Command
element.isDisplayed();
CODE SNIPPET
WebElement element = driver.findElement(By.id("Password"));  
boolean status = element.isDisplayed();  
               
//Or can be written as  
               
boolean staus = driver.findElement(By.id("Password")).isDisplayed();  
#webelement_commands_in_selenium_with_example #Click_Command_in_selenium #Sendkeys_method_in_selenium #Clear_Command #Submit_Method #GetText_Command #GetAttribute_Command #GetLocation_Command #GetTagName_Command #GetSize_Command #IsSelected_Command #IsEnabled #IsDisplayed_method_in_selenium_webdriver

(New page will open, for Comment)

Not yet commented...