0
0
Selenium Pythontesting~10 mins

Find element by ID in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds an element using its ID, clicks it, and verifies the expected page change.

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 TestFindElementByID(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com')

    def test_click_button_by_id(self):
        driver = self.driver
        # Wait until the button with ID 'submit-btn' is present
        button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'submit-btn'))
        )
        button.click()
        # Verify the URL changed to expected page
        WebDriverWait(driver, 10).until(
            EC.url_contains('submit-success')
        )
        self.assertIn('submit-success', driver.current_url)

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open at 'https://example.com'-PASS
2Wait until element with ID 'submit-btn' is presentPage loaded with a button having ID 'submit-btn'Element with ID 'submit-btn' is foundPASS
3Click the button with ID 'submit-btn'Button clicked, page starts navigation-PASS
4Wait until URL contains 'submit-success'Browser URL changes to include 'submit-success'Current URL contains 'submit-success'PASS
5Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Element with ID 'submit-btn' is not found within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to find the element by ID?
Adriver.find_element_by_class_name('submit-btn')
Bdriver.find_element_by_name('submit-btn')
CWebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'submit-btn')))
Ddriver.find_element_by_xpath('//button')
Key Result
Always wait explicitly for elements to be present before interacting to avoid flaky tests caused by timing issues.