Handling checkbox, static dropdowns and other UI elements in Selenium Python
In the previous articles on Selenium Python Tutorial, we have covered “How to handle checkbox, static dropdowns and other UI elements in Selenium Python”. In this tutorial, we will learn How to handle checkbox, static dropdowns, and other UI elements in Selenium Python.
Let us now discuss how to handle common UI elements like edit box, links or buttons, checkbox, and static dropdowns with Selenium Python.
We have to use the send_keys () method to input any text inside an edit box. It is also capable of simulating key presses with the help of the Keys class [selenium.webdriver.common.keys ].
Code Implementation with send_keys () method.
# import the webdriver
from selenium import webdriver
# import the Keys class
from selenium.webdriver.common.keys import webdriver
driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe")
# get method to launch the URL
driver.get("https://www.softwaretestingmaterial.com/sample-webpage-to-automate/")
# to enter text in edit box
driver.find_element_by_name ("username").send_keys ("Python")
# to press a key ENTER
driver.find_element_by_name ("username").send_keys ("Keys.ENTER")
# to close the browser
driver.close()
We have to use the click () method to click on the link, button, checkbox, and radio button.
Code Implementation with click () method to handle button, checkbox, and radio button.
# import the webdriver
from selenium import webdriver
driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe")
# get method to launch the URL
driver.get ("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/")
# to click on checkbox
driver.find_element_by_ css_selector("input[value='cbselenium']").click()
# to click on radio button
driver.find_element_by_ css_selector ("input[value='radioqtp']").click()
# to click on button
driver.find_element_by_name ("spbutton").click()
# to close the browser
driver.close()
Selenium API provides a Select class to handle static dropdown on a web page. Firstly, selenium.webdriver.support.select.Select needs to be imported to work with static dropdowns with <select> tags in HTML.
The methods available under Select class are listed below:
select_by_index (argument)
The option in the static dropdown is selected with the help of an index which matches the method argument. The index of the option starts from 0. NoSuchElementException is shown if there is no matching index to select from the dropdown.
Syntax:
s = Select (driver.find_element_by_name ("sel-option"))
s.select_by_index (0)
select_by_visible_text (argument)
The option in the static dropdown is selected with the help of option visible which matches the method argument. NoSuchElementException is shown if there is no matching visible text to select from the dropdown.
Syntax:
s = Select (driver.find_element_by_name ("sel-option"))
s.select_by_visible_text ('Selenium Python')
select_by_value (argument)
The option in the static dropdown is selected with the help of option value which matches the method argument. NoSuchElementException is shown if there is no matching value to select from the dropdown.
Syntax:
s = Select (driver.find_element_by_name ("sel-option"))
s.select_by_value ('Selenium_Python')
deselect_by_index (argument)
The option in the static dropdown is deselected with the help of the index which matches the method argument. The index of the option starts from 0. NoSuchElementException is shown if there is no matching index to deselect from the dropdown. This method works for only multiple select dropdowns.
Syntax:
s = Select (driver.find_element_by_name ("mul-sel-option"))
s.deselect_by_index (0)
deselect_by_visible_text (argument)
The option in the static dropdown is deselected with the help of option visible which matches the method argument. NoSuchElementException is shown if there is no matching visible text to deselect from the dropdown. This method works for only multiple select dropdowns.
Syntax:
s = Select (driver.find_element_by_name ("mul-sel-option"))
s.deselect_by_visible_text ('Selenium Python')
deselect_by_value (argument)
The option in the static dropdown is deselected with the help of option value which matches the method argument. NoSuchElementException is shown if there is no matching value to deselect from the dropdown. This method works for only multiple select dropdowns.
Syntax:
s = Select (driver.find_element_by_name ("mul-sel-option"))
s.deselect_by_value ('Selenium_Python')
deselect_all ()
All of the selected options are deselected. This method works for only multiple select dropdowns.
Syntax:
s = Select (driver.find_element_by_name ("mul-sel-option"))
s.deselect_all ()
all_selected_options ()
All selected options are returned in the form of a list. This method works for only multiple select dropdowns.
Syntax:
s = Select (driver.find_element_by_name ("mul-sel-option"))
l = s.all_selected_options ()
first_selected_option ()
It shall fetch the first selected option of the dropdown. This method works for both single and multiple select dropdowns. It shall fetch the existing selected option in a single select dropdown and shall fetch the first selected option in a multiple select dropdown.
Syntax:
s = Select (driver.find_element_by_name ("mul-sel-option"))
option = s.first_selected_option ()
options
It shall return all the options of the dropdown in the form of a list.
Syntax:
s = Select (driver.find_element_by_name ("mul-sel-option"))
ops = s.options
Code Implementation with the methods of Select class.
# import the webdriver
from selenium import webdriver
# import the web
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe")
# get method to launch the URL
driver.get ("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/")
# select class provide the methods to handle the options in dropdown
s = Select (driver.find_element_by_xpath("//*[@name='dropdown']"))
# select an option with visible text method
s.select_by_visible_text ("Selenium")
# select an option with index method
s.select_by_index (2)
# select an option with value method
s.select_by_value ("ddmanual")
# browser close
driver.close()
Code Implementation with the methods of Select class for multiple select dropdown.
# import the webdriver
from selenium import webdriver
# import the Select methods
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe")
# get method to launch the URL
driver.get ("https://www.softwaretestingmaterial.com/sample-webpage- to-automate/")
# select class provide the methods to handle the options in dropdown
s = Select (driver.find_element_by_name ("multipleselect[]"))
# select an option with visible text method
s.select_by_visible_text ("Performance Testing")
time.sleep (2)
# deselect an option with visible text method
s.deselect_by_visible_text ("Performance Testing")
time.sleep (2)
# select an option with index method
s.select_by_index (2)
time.sleep (2)
# deselect an option with index method
s.deselect_by_index (2)
time.sleep (2)
# select an option with value method
s.select_by_value ("msselenium")
time.sleep (2)
# deselect an option with value method
s.deselect_by_value ("msselenium")
time.sleep (2)
# select an option with value method
s.select_by_value ("msagile")
time.sleep (2)
# deselect all options
s.deselect_all ()
time.sleep (2)
# to get all options of the dropdown in a list
ls = s.options
# to get the count of options
print (len(ls))
# print the options in console
for k in ls:
print(k.text)
# browser close
driver.close()
In the next article, we will learn JavaScript Executor in Selenium Python
Related posts:
- Selenium Python Tutorial
- Selenium Java Tutorial
- Selenium Interview Questions
- API Testing Tutorial
- Postman Tutorial
