Open In App

Capture all the options in Selenium with Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Prerequisite: Browser Automation Using Selenium

Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we will be using Python.

Requirement:

You need to download install chrome driver from here Click Here and set path. 

Working with Drop-down list:

Initially you have to import the Select class and afterward you have to make the case of Select class. After making the case of Select class, you can perform select strategies on that occasion to choose the choices from dropdown list.

Importing Select class:

from selenium.webdriver.support.ui import Select

Find length of options:

drop=Select(driver.find_element_by_id(' ')

print(len(drop.options))

Step-by-step approach:

  • Import webdriver from selenium module.

Python3




# Import required module
from selenium import webdriver


  • Import Select class module.

Python3




# Importing Select class
from selenium.webdriver.support.ui import Select


  • Using a web page for drop down list(example: URL).
  • Navigate the id of option bar.

  • Start loop for capture all options.

Below is complete program of the above approach:

Python3




# Import required module
import time
from selenium import webdriver
  
# Import Select class
from selenium.webdriver.support.ui import Select
  
  
# Using chrome driver
driver = webdriver.Chrome()
  
# Web page url
  
  
# Find id of option
x = driver.find_element_by_id('RESULT_RadioButton-9')
drop = Select(x)
  
# Count number of options
print(len(drop.options))
  
# Capture all the options
for i in drop.options:
    print(i.text)
  
driver.close()


Output:

Note:One blank space are so print only three options.



Last Updated : 04 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads