Selenium WebDriver Commands/Methods – Web Element methods Part 3

Welcome back to the Selenium Automation Testing Free training series. This will be the last tutorial on WebDriver commands/methods. In the last 2 tutorials, we talked about Browser commands/methods and Navigation commands/methods.

To learn about the methods, please check the following links:

Read Selenium Webdriver Commands/Methods part -1

Read Selenium Webdriver Commands/Methods part – 2

Before jumping on commands/methods directly, let’s have a brief discussion on Web element(s):

What is a web element?

A Web element represents an HTML element. That renders the data into an HTML page. A web page consists of different elements like buttons, input boxes, dropdown lists, links, password box, etc.

To know more about the web elements and what the locators to find the web elements in selenium web driver, please refer to the following link:

Read Locate Web element using Selenium webdriver

Web element methods/commands

All right. Let’s discuss all the web element methods/commands.

1. Clear Command

Method: clear()

This is the Web element’s method that clears the value of input type field web element like name field, password field, email ID field, etc.

Return: void

Parameters: no parameter

Example: 

WebElement name = driver.findElement(By.id("name"));
name.clear();

The above code snippets will get the reference of the name input field and clear the value if any there.

2. Sendkeys Command

Method:  sendKeys( CharSequence? KeysToSend )

This method fills the value to the input field, which is given in the parameter.

Return: void

Parameters: one parameter for text.

Example: 

WebElement name = driver.findElement(By.id("name"));
name.sendKeys("Infotek Solutions");

The above code snippets will get the reference of the name field and provide the value “Infotek Solutions” to the name field.

3. Click Command

Method: click()

This method enables the web driver to click on a button or hyperlink or radio button or checkbox.

Return: void

Parameters: no parameter.

Example: 

WebElement button = driver.findElement(By.id("forms"));
button.click();

The above code snippets locate the element which has ID as forms and performs the click event on the element.

4. IsDisplayed Command

Method: IsDisplayed()

This method helps you check whether a particular web element is displayed on a web page or not.

Return: boolean. If it is displayed, it means it will return true, otherwise false.

Parameters: no parameter

Example: 

WebElement username= driver.findElement(By.id("username"));
boolean isdisplay =  username.isDisplayed();
if(isdisplay){
        //your action code
}

5. IsEnabled Command

Method: isEnabled()

This method is used to check whether the web element is enabled or disabled. It returns true when a web element is enabled within a web page, otherwise false.

Return: boolean

Parameters: no parameter.

Example: 

WebElement username= driver.findElement(By.id("username"));
boolean isEnabled=  username.isEnabled();
if(isEnabled){
        //your action code
}

6. IsSelected Command

Method: IsSelected()

This method checks if the web element is selected or not. It returns true if the web element is selected, otherwise false. You can use this method to check a particular radio or checkbox element is selected.

Return: boolean

Parameters: no parameter

Example: 

WebElement program= driver.findElement(By.xpath("//input[@name='trainingprogram' and @value='qa']"));
boolean qaSelected=  program.isSelected();
if(qaSelected){
        //your action code
}

7. Submit Command

Method: submit()

It works quite similarly to the click method or command in terms of functionalities. But there are some minor differences b/w both. submit() method is only capable to submit the form but the click() method can be used to perform a click event on the button, hyperlink(a tag), checkbox, and radio buttons.

Return: void

Parameters: no parameter

Example: 

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

8. GetText  Command

Method: getText()

This method is used to get the text of a hyperlink or a tag.

Return: String

Parameters: no parameter.

Example: 

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

9. GetTagName Command

Method: getTagName()

To get the tag name of the web element, you can use getTagName() method.

Return: String

Parameters: no parameter.

Example: 

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. getSize Command

Method: getSize()

This method is used to get the size (width and height) of the web element.

Return: Dimension class object

Parameters: no parameter.

Example: 

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

11. getLocation Command

Method: getLocation()

This method is used to get the location(X and Y coordinate) of the web element.

Return: Point class

Parameters: no parameter.

Example: 

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

Instructor-led Training

Important note: we would like to tell you that free tutorials are useful to get started, but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:

For Instructor-led training

Master of Functional Automation Testing

For Self-Driven training

Automation with Selenium WebDriver (Java)

Conclusion

All right. We hope you understood all the web elements methods (that are discussed above) of Selenium WebDriver.  Apart from these, there are some more methods like getCssValue and getAttribute methods. You can use the above methods in different code snippets and test the functionality.

In the next tutorial, we will talk about Managing alerts with the selenium web driver.

Happy Learning until then!

Categories