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
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.
To clear the text present in a textbox use Clear method.
- driver.findElement(By.id("username")).Clear();
To get the text present in textbox use getText method. It returns String value.
- 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.
- String userName= driver.findElement(By.id("username")).getAttribute("value");
Button
HTML syntax
- <input type="submit" />
- <input type="button" />
- <button type="button" />
Below are the methods provided Sy selenium for handling Buttons:
For performing the click operation, we use click method.
- 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).
- driver.findElement(By.id("login")).Submit();
Radio button
HTML syntax
Below are the methods provided by Selenium for handling radio buttons.
For selecting a radio button, we use Click method.
- 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.
- boolean state=driver.findElement(By.id("gender ")).isSelected();
Checkbox
HTML syntax
- <input type="checkbox" />
Below are the methods provided by Selenium for handling Checkbox:
For Checking/Selecting a Checkbox, we use Click method.
- 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.
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- select.deselectByValue("Ind");
If dropdown supports multiple selection then for deselecting all of the selected options we use deselectAll method
- Select select=new Select(driver.findElement(By.id("country")));
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- List<WebElement>list=select.getAllSelectedOptions();
For getting the first selected option in a dropdown we use getFirstSelectedOption method.
This method returns web element.
- Select select=new Select(driver.findElement(By.id("country")));
- 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.
- Select select=new Select(driver.findElement(By.id("country")));
- boolean multipleSelection =select.isMultiple();
Link
HTML syntax
Below are the methods provided by Selenium for handling links:
For performing click operation, we use Click method.
- driver.findElement(By.id("help")).Click();
For getting the link text, we use getText method.
This method returns a string value.
- String linkText = driver.findElement(By.id("help")).getText();
Label
HTML syntax
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.
- 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.