Your ultimate guide

Handling Drop Down using Selenium with Python

Select Class:
  • If we want to work on the drop-down first we need to create object an instance of the 'Select' Class.
    • Syntax: object = Select(dropdown_element)
    • Example:
      1. dropdown = driver.find_element(By.ID, 'tcg')
      2. obj = Select(dropdown)
    • Example 2:
      1. obj = Select(driver.find_element(By.ID, 'tcg'))
  • Select class interacts with dropdown elements in web pages.
  • Select Class contain different functions by using that function we can handle the dropdown elements.
    • Select Class functions are:
    1. options 
      • Returns a list of all options to this select tag
    2. all_selected_options(self) 
      • Return a list of all selected options to this select tag
    3. first_selected_option(self) 
      • The first selected option in this select tag
    4. select_by_value(self, value: str)
      • Select all options that have a value matching the argument.
    5. select_by_index(self, index: int)
      • Select the option at the given index.
    6. select_by_visible_text(self, text: str)
      • Select all options that display text matching the argument.
    7. deselect_all(self)
      • Clear all selected entries. This is only valid when the SELECT supports multiple selections.
    8. deselect_by_value(self, value:str)
      • Deselect all options that have a value matching the argument.
    9. deselect_by_index(self, value: int)
      • Deselect the option at the given index.
    10. deselect_by_visible_text(self, text:str)
      • Deselect all options that display text matching the argument.
Note: The Select Class functions are designed specifically for dropdowns on a web page created using the select tag.



Let's explore each function in a Select class in detail. Refer to the image below for the source code and output of a dropdown.





options:
    options function allows you to retrieve all the available options within a dropdown element.

Here's how you can use the option method in the Select class:
  1. Import the necessary modules:
    • from selenium import webdriver
    • from selenium.webdriver.support.select import Select
  2. Instantiate a WebDriver instance (e.g., ChromeDriver) and navigate to a web page:
    • driver = webdriver.Chrome()
    • driver.get("https://example.com")
  3. Locate the dropdown element on the page.
    • dropdown = driver.find_element(By.ID, 'tcg')
  4. Create a 'Select' object and use it to interact with the dropdown.
    • select = Select(dropdown)
  5. Then use the 'options' function to retrieve all the available options as a list of 'WebElement' objects:
    • all_options = select.options
  6. Now, all_options will contain a list of WebElement objects, each representing an option within the dropdown.
  7. You can then perform various operations on these options, such as selecting an option, getting the text or value of an option, etc.
  8. For example, to print the text of all options:
    • for option in all_options:
    •     print(option.text)



all_selected_options:
    This function is used to retrieve all the selected options from a multi-select dropdown or list on a web page.

Here's how you can use it:
  1. Import the necessary modules:
    • from selenium import webdriver
    • from selenium.webdriver.support.select import Select
  2. Instantiate a WebDriver instance (e.g., ChromeDriver) and navigate to a web page:
    • driver = webdriver.Chrome()
    • driver.get("https://example.com")
  3. Locate the multi-select dropdown element you want to interact with.
    • dropdown = driver.find_element(By.ID, 'tcg')
  4. Create a 'Select' object and use it to interact with the dropdown.
    • select = Select(dropdown)
  5. Then use the 'all_selected_options' function to retrieve all the selected options as a list of 'WebElement' objects:
    • selected_options = select.all_selected_options
  6. Now, selected_options will contain a list of WebElement objects.
  7. You can then perform various operations on these options, such as unselecting the option, getting the text or value of an option, etc.
  8. For example, to print the text of all selected options:
    • for option in selected_options:
    •     print(option.text)



first_selected_option:
    The 'fist_selected_option' function is used to retrieve the first selected option from a dropdown or list. 

Here's how you can use it:
  1. Import the necessary modules:
    • from selenium import webdriver
    • from selenium.webdriver.support.select import Select
  2. Instantiate a WebDriver instance (e.g., ChromeDriver) and navigate to a web page:
    • driver = webdriver.Chrome()
    • driver.get("https://example.com")
  3. Locate the dropdown element you want to interact with.
    • dropdown = driver.find_element(By.ID, 'tcg')
  4. Create a 'Select' object and use it to interact with the dropdown.
    • select = Select(dropdown)
  5. Then use the 'first_selected_option' function to retrieve the first selected option as a WebElement object:
    • first_option = select.first_selected_option
  6. You can then access the text or other attributes of the first selected option:
    • print(first_option.text)



select_by_value('option_value'):
    The 'select_by_value' method is used to select an element by specifying its 'value' attribute in the dropdown.

Here's how you can use it:
  1. Import the necessary modules:
    • from selenium import webdriver
    • from selenium.webdriver.support.select import Select
  2. Instantiate a WebDriver instance (e.g., ChromeDriver) and navigate to a web page:
    • driver = webdriver.Chrome()
    • driver.get("https://example.com")
  3. Locate the dropdown element you want to interact with.
    • dropdown = driver.find_element(By.ID, 'tcg')
  4. Create a 'Select' object and use it to interact with the dropdown.
    • select = Select(dropdown)
  5. Then use the 'select_by_value' function to select an option by specifying its 'value' attribute:
    • select.select_by_value('option_value')
deselect_by_value:
    This function is exactly the opposite of the 'select_by_value' function. This is used to deselect an element by specifying its 'value' attribute in the dropdown.
    So in the above example instead of giving 'select_by_value' if you give 'deselect_by_value' then that option will be deselected from the multi-select dropdown.



select_by_index:
    The 'select_by_index' method is used to select an option in the dropdown by specifying its index. The index is a zero-based integer that represents the position of the option in the dropdown list.

Here's how you can use it:
  1. Import the necessary modules:
    • from selenium import webdriver
    • from selenium.webdriver.support.select import Select
  2. Instantiate a WebDriver instance (e.g., ChromeDriver) and navigate to a web page:
    • driver = webdriver.Chrome()
    • driver.get("https://example.com")
  3. Locate the dropdown element you want to interact with.
    • dropdown = driver.find_element(By.ID, 'tcg') //replace with the actual ID of your dropdown element.
  4. Create a 'Select' object and use it to interact with the dropdown.
    • select = Select(dropdown)
  5. Use the 'select_by_index' method to select an option by specifying its index.
    • select.select_by_index(3)
  6. Then use the 'select_by_index(3)' function will select the fourth option(since the index is zero-based) in the dropdown.
Similarly, we have the 'deselect_by_index' function to deselect an option by specifying its index.



select_by_visible_text:
    The 'select_by_visible_text' function is used to select an option from a dropdown by specifying its visible text (the text that is displayed to the user).

Here's how you can use it:
  1. Import the necessary modules:
    • from selenium import webdriver
    • from selenium.webdriver.support.select import Select
  2. Instantiate a WebDriver instance (e.g., ChromeDriver) and navigate to a web page:
    • driver = webdriver.Chrome()
    • driver.get("https://example.com")
  3. Locate the dropdown element you want to interact with.
    • dropdown = driver.find_element(By.ID, 'tcg')
  4. Create a 'Select' object and use it to interact with the dropdown.
    • select = Select(dropdown)
  5. Then use the 'select_by_visible_text' function to select an option by specifying its visible text:
    • select.select_by_visible_text('option_text')
  6. Then it will select the option with the text "option_text" in the dropdown.

Similarly, we have a function to deselect the option in the dropdown by using visible text that is 'deselect_by_visible_text'.



deselect_all:
    The 'deselect_all' function is used to deselect all selected options in a multi-select dropdown.

Here's how you can use it:
  1. Import the necessary modules:
    • from selenium import webdriver
    • from selenium.webdriver.support.select import Select
  2. Instantiate a WebDriver instance (e.g., ChromeDriver) and navigate to a web page:
    • driver = webdriver.Chrome()
    • driver.get("https://example.com")
  3. Locate the dropdown element you want to interact with.
    • dropdown = driver.find_element(By.ID, 'tcg')
  4. Create a 'Select' object and use it to interact with the multi-select dropdown.
    • select = Select(dropdown)
  5. Then use the 'deselect_all' function to deselect all selected options in the dropdown:
    • select.deselect_all()
  6. After calling 'select.deselect_all()', all previously selected options in the multi-select dropdown will be deselected.


No comments:

Post a Comment