0
0
Selenium Pythontesting~10 mins

Select by value, text, index in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage with a dropdown menu and selects options using three methods: by value, by visible text, and by index. It verifies that the correct option is selected each time.

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 TestSelectMethods(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select')
        self.driver.switch_to.frame('iframeResult')

    def test_select_methods(self):
        select_element = self.driver.find_element(By.ID, 'cars')
        select = Select(select_element)

        # Select by value
        select.select_by_value('volvo')
        selected_option = select.first_selected_option
        self.assertEqual(selected_option.get_attribute('value'), 'volvo')

        # Select by visible text
        select.select_by_visible_text('Saab')
        selected_option = select.first_selected_option
        self.assertEqual(selected_option.text, 'Saab')

        # Select by index
        select.select_by_index(3)  # Index 3 corresponds to 'Audi'
        selected_option = select.first_selected_option
        self.assertEqual(selected_option.text, 'Audi')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser is open with blank page-PASS
2Navigates to 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select'Page loads with iframe containing dropdown-PASS
3Switches to iframe 'iframeResult'Inside iframe, dropdown with id 'cars' is visible-PASS
4Finds dropdown element by ID 'cars'Dropdown element located-PASS
5Selects option by value 'volvo'Dropdown option 'Volvo' is selectedCheck selected option's value is 'volvo'PASS
6Selects option by visible text 'Saab'Dropdown option 'Saab' is selectedCheck selected option's text is 'Saab'PASS
7Selects option by index 3Dropdown option 'Audi' is selectedCheck selected option's text is 'Audi'PASS
8Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Dropdown element with ID 'cars' is not found or option value/text/index does not exist
Execution Trace Quiz - 3 Questions
Test your understanding
Which method is used to select the option with value 'volvo'?
Aselect.select_by_value('volvo')
Bselect.select_by_visible_text('volvo')
Cselect.select_by_index('volvo')
Dselect.select('volvo')
Key Result
Always switch to the correct iframe before interacting with elements inside it, and verify selections by checking the selected option's value or text to ensure the correct item is chosen.