0
0
Selenium Pythontesting~10 mins

Select class for dropdowns in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage with a dropdown menu, selects an option by visible text using Selenium's Select class, and verifies the correct option is selected.

Test Code - unittest
Selenium Python
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import unittest

class TestDropdownSelection(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/dropdown')

    def test_select_option_by_visible_text(self):
        driver = self.driver
        dropdown_element = driver.find_element(By.ID, 'dropdown')
        select = Select(dropdown_element)
        select.select_by_visible_text('Option 2')
        selected_option = select.first_selected_option
        self.assertEqual(selected_option.text, 'Option 2')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to navigate-PASS
2Browser navigates to 'https://example.com/dropdown'Page with dropdown menu loaded-PASS
3Find dropdown element by ID 'dropdown'Dropdown element located on the page-PASS
4Create Select object for dropdown elementSelect object ready to interact with dropdown-PASS
5Select option with visible text 'Option 2'Dropdown option 'Option 2' is selectedVerify selected option text is 'Option 2'PASS
6Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Dropdown element with ID 'dropdown' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
Which Selenium method is used to find the dropdown element?
Afind_element(By.ID, 'dropdown')
Bfind_element(By.CLASS_NAME, 'dropdown')
Cfind_element(By.NAME, 'dropdown')
Dfind_element(By.TAG_NAME, 'select')
Key Result
Use Selenium's Select class to interact with dropdown menus by selecting options with visible text, which is more readable and less error-prone than selecting by index or value.