Handling Different Types Of Web Elements Using Selenium

Web applications will have different types of elements in them, including: 

  • Textbox
  • Button
  • Radio button
  • Checkbox
  • Dropdown
  • Link
  • Label

Each element will have a different HTML tag.

In this article, I’ll explain how to handle these elements using Selenium.

Textbox

HTML syntax

  1. <input type="text" />  

Below are the methods provided by Selenium for handling Textbox.

  • sendKeys(java.lang.CharSequence... keysToSend)
  • Clear()
  • getText()

For entering text into a textbox or to set a value in a textbox, Selenium has provided a method called sendKeys. It will accept charsequense/String as input parameter.

  1. driver.findElement(By.id("username")).sendKeys("[email protected]");  

To clear the text present in a textbox use Clear method.

  1. driver.findElement(By.id("username")).Clear();  

To get the text present in textbox use getText method. It returns String value.

  1. String userName= driver.findElement(By.id("username")).getText();  

Sometimes, the text will be stored in value property of a textbox.

So, to get the value present in value property we use getAttribute method.

  1. String userName= driver.findElement(By.id("username")).getAttribute("value");   

Button

HTML syntax

  1. <input type="submit" />  
  2.    <input type="button" />  
  3. <button type="button" />  

Below are the methods provided Sy selenium for handling Buttons:

  • Click()
  • Submit()

For performing the click operation, we use click method.

  1. driver.findElement(By.id("login")).Click();  

For submitting the page to remote server, we use submit method (if the element is a form or is present inside the form).

  1. driver.findElement(By.id("login")).Submit();  

Radio button

HTML syntax

  1. <input type="radio" />  

Below are the methods provided by Selenium for handling radio buttons.

  • Click()
  • isSelected()

For selecting a radio button, we use Click method.

  1. driver.findElement(By.id("gender")).Click();  

To find out the state of a radio button, whether it is selected or not, we use isSelected method. This method returns Boolean value.

If the radio button is selected, then it will return True else it will return False.

  1. boolean state=driver.findElement(By.id("gender ")).isSelected();   

Checkbox

HTML syntax

  1. <input type="checkbox" />  

Below are the methods provided by Selenium for handling Checkbox:

  • Click()
  • isSelected()

For Checking/Selecting a Checkbox, we use Click method.

  1. driver.findElement(By.id("vehicle ")).Click();  

To find out the state of a checkbox (whether it is selected or not) we use isSelected method. This method returns Boolean value.

If the checkbox is checked, then it will return True; else it will return False.

  1. boolean state=driver.findElement(By.id("vehicle ")).isSelected();  

Dropdown

HTML syntax

<select />

Below are the methods provided by Selenium for handling Dropdown:

  • selectByVisibleText(String)
  • selectByIndex(int)
  • selectByValue(String)
  • deselectByIndex(int)
  • deselectByValue(String)
  • deselectByVisibleText(String)
  • deselectAll()
  • getAllSelectedOptions()
  • getFirstSelectedOption()
  • getOptions()
  • isMultiple()

For selecting the option in dropdown using visible text we use selectByVisibleText method.

This method accepts string value as a parameter. Here we need to pass visible text to this method.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. select.selectByVisibleText("India");  

For selecting the option in dropdown using its index we use selectByIndex method.

This method accepts integer value as a parameter. Here we need to pass option index to this method.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. select.selectByIndex(0);  

For selecting the option in dropdown using option value we use selectByValue method.

This method accepts string value as a parameter. Here we need to pass option value to this method.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. select.selectByValue("Ind");  

All of the above three methods are used for selecting options in the dropdown.

The below four methods are for deselecting the option in the drop-down.

For deselecting the option in drop-down using visible text we use deselectByVisibleText method.

This method accepts string value as a parameter. Here we need to pass visible text to this method.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. select.deselectByVisibleText("India");  

For deselecting the option in dropdown using its index we use deselectByIndex method.

This method accepts integer value as a parameter. Here we need to pass option index to this method.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. select.deselectByIndex(0);  

For deselecting the option in dropdown using option value we use deselectByValue method.

This method accepts string value as a parameter. Here, we need to pass option value to this method.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. select.deselectByValue("Ind");  

If dropdown supports multiple selection then for deselecting all of the selected options we use deselectAll method

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. select.deselectAll();  

For getting the options present in a dropdown we use getOptions method.

This method will return a list of Web elements.

Each element in this list will represent one option in the drop-down.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. List<WebElement> list=select.getOptions();  

If dropdown supports multiple selection then for getting the selected options in a dropdown we use getAllSelectedOptions method.

This method returns a list of web elements.

Each element in this list will represent an option in the drop-down.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. List<WebElement>list=select.getAllSelectedOptions();  

For getting the first selected option in a dropdown we use getFirstSelectedOption method.

This method returns web element.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. WebElement webElement =select.getFirstSelectedOption();  

To know whether dropdown supports multiple selection at the same time or not we use isMultiple method.

This method returns a Boolean value.

If the drop-down supports multiple selections then it will return true else it will return false.

  1. Select select=new Select(driver.findElement(By.id("country")));  
  2. boolean multipleSelection =select.isMultiple();  

Link

HTML syntax

  1. <a href="#" />  

Below are the methods provided by Selenium for handling links:

  • Click()
  • getText()

For performing click operation, we use Click method.

  1. driver.findElement(By.id("help")).Click();  

For getting the link text, we use getText method.

This method returns a string value.

  1. String linkText = driver.findElement(By.id("help")).getText();  
Label

HTML syntax

  1. <p />  
  2. <span />  
  3. <label />  

Below are the methods provided by Selenium for handling labels.

getText()

For getting the label text we use getText method.

This method returns a string value.

  1. String labelText = driver.findElement(By.id("usernamelbl")).getText();  

If you are interested in watching this full information explained in a video, please watch the below videos: 

To handle basic HTML controls like textbox, radio button, checkbox, button and hyperlink, Watch this video.
 
To handle HTML dropdowns(single select and multi-select), Watch this video
 
I hope you find this article useful. Please provide your valuable feedback, questions, or comments about this article.


Similar Articles