0
0
Selenium Pythontesting~10 mins

Modifying element attributes with JS in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds a button element, modifies its 'disabled' attribute using JavaScript, and verifies that the button is enabled afterwards.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

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

    def test_enable_button_with_js(self):
        driver = self.driver
        wait = WebDriverWait(driver, 10)
        # Wait until button is present
        button = wait.until(EC.presence_of_element_located((By.ID, 'submit-btn')))
        # Initially button is disabled
        self.assertTrue(button.get_attribute('disabled') is not None)
        # Use JS to remove 'disabled' attribute
        driver.execute_script("arguments[0].removeAttribute('disabled')", button)
        # Verify button is enabled
        self.assertIsNone(button.get_attribute('disabled'))

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to load the test page-PASS
2Navigates to 'https://example.com/testpage'Page loads with a button element having id 'submit-btn' and attribute 'disabled'-PASS
3Waits until the button with id 'submit-btn' is presentButton element is found in the DOMButton element is located successfullyPASS
4Checks that the button has 'disabled' attribute initiallyButton is disabled on the pageAssert button.get_attribute('disabled') is not NonePASS
5Executes JavaScript to remove 'disabled' attribute from the buttonButton attribute 'disabled' is removed, button becomes enabled-PASS
6Checks that the button no longer has 'disabled' attributeButton is enabled and clickableAssert button.get_attribute('disabled') is NonePASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Button element with id 'submit-btn' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do to enable the disabled button?
AClicks the button to enable it
BRemoves the 'disabled' attribute using JavaScript
CReloads the page to reset the button
DChanges the button text
Key Result
Using JavaScript to modify element attributes can help test UI states that are otherwise hard to change through normal user actions.