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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | — | PASS |
| 2 | Browser navigates to 'https://example.com' | Page 'https://example.com' is loaded in the browser | — | PASS |
| 3 | Find 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 |
| 4 | Get 'class' attribute of the button element using get_attribute('class') | Retrieved attribute value is 'btn primary' | Check if attribute value equals 'btn primary' | PASS |
| 5 | Assert that the class attribute equals 'btn primary' using self.assertEqual | Assertion compares actual and expected attribute values | class_attr == 'btn primary' | PASS |
| 6 | Close the browser and end the test | Browser window is closed | — | PASS |
get_attribute('href') do when called on a web element?get_attribute is used to read the value of a specified attribute from a web element.element using Selenium in Python?get_attribute with an underscore, all lowercase.get_attribute('class'), which matches the correct syntax exactly.element = driver.find_element(By.ID, 'submit-btn')
value = element.get_attribute('type')
print(value)<button id='submit-btn' type='submit'>Send</button>, what will be printed?type='submit'.get_attribute('type') returns the string 'submit' as that is the attribute's value.element = driver.find_element(By.NAME, 'email')
email_value = element.get_attribute('value')
print(email_value)None even though the input field has a value. What is the most likely cause?<input type='checkbox' id='agree' checked>
Pass the test and this pattern moves unknown