This program opens a simple dropdown with three fruit options. It selects options by value, text, and index, printing the selected option each time.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
# Setup driver (using Chrome here)
driver = webdriver.Chrome()
# Open a simple page with a dropdown
html = '''
<html>
<body>
<select id='fruits'>
<option value='apple'>Apple</option>
<option value='banana'>Banana</option>
<option value='cherry'>Cherry</option>
</select>
</body>
</html>
'''
# Load the HTML content
driver.get('data:text/html;charset=utf-8,' + html)
# Find the select element
select_element = driver.find_element(By.ID, 'fruits')
# Create Select object
select = Select(select_element)
# Select by value
select.select_by_value('banana')
print('Selected by value:', select.first_selected_option.text)
# Select by visible text
select.select_by_visible_text('Cherry')
print('Selected by text:', select.first_selected_option.text)
# Select by index
select.select_by_index(0)
print('Selected by index:', select.first_selected_option.text)
# Close driver
driver.quit()