How to Handling drop down in Selenium
you might have to select some values from the drop-down list and
also perform other activities and validate them. For that in Selenium ‘Select’
Class is used to selecting and deselecting options from drop down.
The Select class is a Webdriver class and you should pass dropdown
web element as parameter to it constructor.
We need import ‘Select’ Class as
from selenium.webdriver.support.ui import Select
Syntax for Select class is below
dropdown=Select(<WebElement>)
Ex:
Day_ID=driver.find_element_by_id("day")
daydropdown=Select(Day_ID)
To handle dropdown options in selenium we have three different
methods are available .
1. select_by_visible_text
2. select_by_index
3. select_by_value
select_by_visible_text: It is used to select the dropdown option based on text.
Ex: daydropdown.select_by_visible_text("28")
Select_by_index(): It is used to select the dropdown options based on its index, beginning
with ‘0’.
Ex: daydropdown.select_by_index(2).
Select_by_value(): It is used to select the dropdown options based on value
attribute provided by developer.
Ex: daydropdown.select_by_value(1)
Below is the example , it will cover all three methods.
Scenario:
Invoke Google Chrome
browser.
Select date of birth from
Birthday section.
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.facebook.com/")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.facebook.com/")
# Select day using visible by text
method
dayid=driver.find_element_by_id("day")
daydropdown=Select(dayid)
daydropdown.select_by_visible_text("28")
dayid=driver.find_element_by_id("day")
daydropdown=Select(dayid)
daydropdown.select_by_visible_text("28")
#Select Month using by_index method
monthid=driver.find_element_by_id("month")
mothdropdown=Select(monthid)
mothdropdown.select_by_index(9)
monthid=driver.find_element_by_id("month")
mothdropdown=Select(monthid)
mothdropdown.select_by_index(9)
#Select year using by_value method
yearid=driver.find_element_by_id("year")
yeardropdown=Select(yearid)
yeardropdown.select_by_value("2017")
yearid=driver.find_element_by_id("year")
yeardropdown=Select(yearid)
yeardropdown.select_by_value("2017")
options():
some
time you want to print all option items from the dropdown, in this case you can
use options() method.
Below is the example to
print all months in months dropdown using options() method.
Scenario:
Invoke Google Chrome
browser.
Print all months from
month dropdown.
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.facebook.com/")
monthid=driver.find_element_by_id("month")
monthdd=Select(monthid)
for item in monthdd.options:
print(item.text,end=" : ")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.facebook.com/")
monthid=driver.find_element_by_id("month")
monthdd=Select(monthid)
for item in monthdd.options:
print(item.text,end=" : ")
Output:
Month : Jan : Feb : Mar : Apr : May : Jun : Jul : Aug : Sept : Oct
: Nov : Dec :
How to handle multiple items in a multi select
dropdown.
There is no separate
methods for selecting multiple items in multi select dropdown. So you can use any
one of the selectByVisibleText(),selectbyindex() or selectbyvalue() methods in
selecting multiple options in a multi SELECT element.
As an example, we will take https://mdbootstrap.com/docs/jquery/forms/multiselect/
as the base URL. It
contains a drop-down box that allows multiple selections at a time.
You should use multiple
selectbyvisibletext() or selectbyindex() or selectbyvalue() methods.
Below is the example for
selecting multiple items.
Note: if SELECT tag has
“multiple” attribute means it will support multi selection.
I want to select “Open
this select menu”, “Two” and “Three” items from dropdown.
Script:
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
selectname=driver.find_element_by_xpath("//select[@class='custom-select
browser-default']")
selectcars=Select(selectname)
selectcars.select_by_visible_text("Two")
selectcars.select_by_index("0")
selectcars.select_by_value(“3”)
selectcars.select_by_index("0")
selectcars.select_by_value(“3”)
below is the another alternative code for multi select.
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
# define list of selected items in the
list
List=["Two","Open
this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select
browser-default']")
selectcars=Select(selectname)
for item in List:
selectcars.select_by_visible_text(item)
selectcars.select_by_visible_text(item)
is_multiple():This method tells whether the SELECT element supports multiple
selection options at the same time or not. It returns TRUE if the drop-down element allows multiple selections at
a time else FALSE.
Scearnio :
check whether month select dropdown is multi select or
not in facebook application.
Below is the code for is_multiple() method.
Below is the code for is_multiple() method.
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.facebook.com/")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://www.facebook.com/")
monthid=driver.find_element_by_id("month")
mothdropdown=Select(monthid)
bool=mothdropdown.is_multiple
mothdropdown=Select(monthid)
bool=mothdropdown.is_multiple
if(bool):
print("drop down is multi select")
else:
print("drop down is not multi select")
print("drop down is multi select")
else:
print("drop down is not multi select")
DeSelect methods:
When you select a particular element on the webpage, there are a
few methods are available to deselecting that element. These methods are work
only on multiselect elements, not for dropdowns elements.
The following are the most common used deselected methods.
1) deselect_all()
2) deselect_by_index()
3) deselect_by_value()
4) deselect_by_visible_text()
deselect_all():
It clears all selected elements. This is only valid when the drop-down element supports multiple selections.
Ex: dropdown.deselect_all()
Below is the example to deselect the all selected elements.
Script:
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
List=["Two","Open this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select browser-default']")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
List=["Two","Open this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select browser-default']")
selectcars=Select(selectname)
# select multiple items at a time
for item in List:
selectcars.select_by_visible_text(item)
for item in List:
selectcars.select_by_visible_text(item)
# deselect the previously selected items
at a time.
selectcars.deselect_all()
selectcars.deselect_all()
Deselect_by_index(): it will deselect the option
at a given index.
Ex: dropdown.deselect_by_index(2)
Below is the example to deselect the element based on index.
Scenario: I want to deselect “Two” from the previously selected item”two.
Script:
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")List=["Two","Open this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select browser-default']")
selectcars=Select(selectname)
for item in List:
selectcars.select_by_visible_text(item)
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")List=["Two","Open this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select browser-default']")
selectcars=Select(selectname)
for item in List:
selectcars.select_by_visible_text(item)
# deselect the item from the drop down
selectcars.deselect_by_index(2)
selectcars.deselect_by_index(2)
Deselect_by_value(): It is used to deselect the dropdown options based on value
attribute provided by developer.
Ex: dropdown.deselect_by_value(2)
Below is the example to deselect the element based on value.
Scenario: I want to deselect “Two” from the previously selected item.
Script:
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
List=["Two","Open this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select browser-default']")
selectcars=Select(selectname)
for item in List:
selectcars.select_by_visible_text(item)
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
List=["Two","Open this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select browser-default']")
selectcars=Select(selectname)
for item in List:
selectcars.select_by_visible_text(item)
#
deselect the item from the drop down
selectcars.deselect_by_value(“2”)
selectcars.deselect_by_value(“2”)
Deselect_by_visible_text(): It is used to deselect the dropdown options based on the text matching
the parameter.
Ex: dropdown.deselect_by_visible_text(“two”)
Below is the example to deselect the element based on visible text.
Scenario: I want to deselect “Two” from the previously selected item.
Script:
from selenium
import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
List=["Two","Open this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select browser-default']")
selectcars=Select(selectname)
for item in List:
selectcars.select_by_visible_text(item)
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://mdbootstrap.com/docs/jquery/forms/multiselect/")
List=["Two","Open this select menu","Three"]
selectname=driver.find_element_by_xpath("//select[@class='custom-select browser-default']")
selectcars=Select(selectname)
for item in List:
selectcars.select_by_visible_text(item)
#
deselect the item from the drop down
selectcars.deselect_by_visible_text("Two")
selectcars.deselect_by_visible_text("Two")
Comments
Post a Comment