0
0
Selenium Pythontesting~10 mins

Getting element attributes in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds a button element by its ID, retrieves its 'class' attribute, and verifies that the attribute value matches the expected class name.

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

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

    def test_button_class_attribute(self):
        button = self.driver.find_element(By.ID, 'submit-btn')
        class_attr = button.get_attribute('class')
        self.assertEqual(class_attr, 'btn primary')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Browser navigates to 'https://example.com'Page 'https://example.com' is loaded in the browser-PASS
3Find element with ID 'submit-btn' using driver.find_element(By.ID, 'submit-btn')Button element with ID 'submit-btn' is located on the page-PASS
4Get 'class' attribute of the button element using get_attribute('class')Retrieved attribute value is 'btn primary'Check if attribute value equals 'btn primary'PASS
5Assert that the class attribute equals 'btn primary' using self.assertEqualAssertion compares actual and expected attribute valuesclass_attr == 'btn primary'PASS
6Close the browser and end the testBrowser window is closed-PASS
Failure Scenario
Failing Condition: Element with ID 'submit-btn' is not found or the 'class' attribute value is different than expected
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to find the button element by its ID?
Adriver.find_element(By.ID, 'submit-btn')
Bdriver.get_attribute('submit-btn')
Cdriver.find_elements(By.CLASS_NAME, 'btn')
Ddriver.click('submit-btn')
Key Result
Always verify element attributes by retrieving them with get_attribute and asserting their expected values to ensure UI elements have correct properties.