WebElements Commands

WebElement represents an HTML element. They can be written with a start tag and end tag with the content in between: <tagname> Web Page content </tagname>.


There are many commonly used web element commands and actions in selenium web driver.

1. Find Element Command

This method is used to locate the first matching element on the webpage. It takes, By object as the parameter and returns an object of type WebElement. By object is in turn used with various locator strategies such as ID, Name, Class Name, XPATH etc. This method throws NoSuchElementException if there are no such elements found. Below is the syntax of FindElement command in selenium web driver.

WebElement elementName = driver.findElement(By.LocatorStrategy(“LocatorValue”));

Example: WebElement element = driver.findElement(By.id(“side-menu”));
 


2. Find Elements Command

This method is used to locate all the webelements in the webpage. It takes, By object as a parameter and returns a list of webelements. It returns an empty list if no matching element is found using the given locator strategy and locator value. Below is the syntax of FindElements command in selenium webdriver.

List<WebElement> elementName = driver.findElements(By.LocatorStrategy(“LocatorValue”));

Example: List<WebElement> elements = driver.findElements(By.className(“nav-second-level”));
 


3. Click Command

This method emulates to perform various mouse-based operations for your web-application such as click operation for a link, button, textbox or image. It takes a locator as an argument. Below is the syntax of FindElements command in selenium webdriver.

driver.findElement(By.LocatorStrategy(“LocatorValue”)).getText();

Example: driver.findElement(By.className(“panel-heading”)).getText();
 


4. SendKeys Command

This method emulates to type the content automatically into an editable field like textbox, text area etc. This method passes the content into editable field without replacing previous value. It takes string as a parameter. Below is the syntax of FindElements command in selenium webdriver.

driver.findElement(By.LocatorStrategy(“LocatorValue”)).sendKeys(String);

Example: driver.findElement(By.id(“text input”)).sendKeys(“123”);
 


5. Get Text Command

This method is used to get the innertext (i.e. not hidden by CSS) of webelement, even sub-elements, without any leading white space. It doesn’t accept anything as a parameter and returns string value. This method is widely used to verify the validation messages, labels etc. . Below is the syntax of FindElement command in selenium webdriver.

driver.findElement(By.LocatorStrategy(“LocatorValue”)).getText();

Example: driver.findElement(By.className(“panel-heading”)).getText();
 


6. Get Attribute Command

This method is used to get the value of the specified attribute. It accepts string as a parameter and returns also string value. Below is the syntax of FindElement command in selenium webdriver. Below is the syntax of FindElement command in selenium webdriver.

driver.findElement(By.LocatorStrategy(“LocatorValue”)).getAttribute(“AttributeName”);

Example: driver.findElement(By.id(“optionsRadios1”)).getAttribute(“value”);
 


7. Get Size Command

This method returns the width and height of the rendered WebElement. It accepts nothing as a parameter and return dimension class. Below is the syntax of FindElements command in selenium webdriver.

driver.findElements(By.LocatorStrategy(“LocatorValue”)).getSize();

Example: driver.findElement(By.className(“nav-second-level”)).getSize();
 


8. Get Tag Name Command

This method returns the tag name of the web element. For example, if the element is an <img> the tag name returned is “IMG”. This method doesn’t take any parameter and return the tagname of the specified element.

driver.findElements(By.LocatorStrategy(“LocatorValue”)). getTagName();

Example:  driver.findElement(By.tagName(“input”)).sendKeys(“123”);
 


9. Get Css Value Command

This method fetches the value of a CSS property of a web element. CSS values depend on the browser, you may not get the same values for all the browser.

driver.findElements(By.LocatorStrategy(“LocatorValue).getCssValue(“AttributeName”);

Example: driver.findElement(By.xpath(“//button[contains(text(), Primary”)).getCssValue(“color”);
 


10. IsDisplayed Command

isDisplayed() is the method used to verify presence of a web element within the webpage. This method returns Boolean value. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.

driver.findElements(By.LocatorStrategy(“LocatorValue). isDisplayed();

Example: driver.findElement(By.xpath(“//button[@type=’submit’]”)).isDisplayed();
 


11. IsEnabled() Command

isEnabled() is the method used to verify if the web element is enabled or disabled within the webpage. This method returns Boolean value. The method returns a “true” value if the specified web element is enabled on the web page and a “false” value if the web element is not enabled on the web page.

driver.findElements(By.LocatorStrategy(“LocatorValue). isEnabled();

Example: driver.findElement(By.id(“dcbutton”)).isEnabled();
 


12. IsSelected() Command

isSelected() is the method used to verify if the web element is selected or not. isSelected() method is used with radio buttons, dropdowns and checkboxes. This method returns Boolean value. The method returns a “true” value if the specified web element is selected on the web page and a “false” value if the web element is not selected on the web page.

driver.findElements(By.LocatorStrategy(“LocatorValue). isSelected();

Example: driver.findElement(By.id(“optionsRadios1”)).isSelected();