0
0
Selenium Pythontesting~10 mins

XPath with attributes in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage, finds a button using XPath with an attribute, clicks it, and verifies the expected text appears.

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

    def test_click_button_by_xpath_attribute(self):
        driver = self.driver
        # Wait until button with attribute id='submit-btn' is present
        button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//button[@id='submit-btn']"))
        )
        button.click()
        # Verify that a div with id='result' contains text 'Success'
        result_div = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "//div[@id='result']"))
        )
        self.assertIn('Success', result_div.text)

    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 and ready-PASS
2Browser navigates to 'https://example.com/testpage'Page loads with a button having id='submit-btn' and a hidden result div-PASS
3Wait until button with XPath //button[@id='submit-btn'] is presentButton is found on the pageButton element is located successfullyPASS
4Click the button found by XPathButton is clicked, triggering page update-PASS
5Wait until div with XPath //div[@id='result'] is visibleResult div appears with text contentResult div is visiblePASS
6Check that result div text contains 'Success'Result div text is 'Success'Assert 'Success' in result_div.textPASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Button with id='submit-btn' is not found within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
Which XPath expression is used to find the button in this test?
A//button[@id='submit-btn']
B//div[@id='submit-btn']
C//button[@class='submit-btn']
D//input[@id='submit-btn']
Key Result
Using XPath with attributes allows precise element location, improving test reliability and readability.