XPath with attributes helps you find specific elements on a web page by looking at their properties. This makes your tests more accurate and reliable.
XPath with attributes in Selenium Python
//tagname[@attribute='value']Use double slashes // to search anywhere in the page.
Attributes are inside square brackets with @ before the attribute name.
//input[@id='username']
//button[@class='submit-btn']//a[@href='https://example.com']//div[@data-test='login-form']This script opens a browser, goes to a login page, finds the username input using XPath with an id attribute, checks if it is visible, prints a success message, and closes the browser.
from selenium import webdriver from selenium.webdriver.common.by import By # Start the browser browser = webdriver.Chrome() # Open a simple test page browser.get('https://example.com/login') # Find the username input by its id attribute username_input = browser.find_element(By.XPATH, "//input[@id='username']") # Check if the input is displayed assert username_input.is_displayed(), 'Username input should be visible' print('Test passed: Username input found and visible') # Close the browser browser.quit()
Always use unique attributes to avoid selecting the wrong element.
Use browser developer tools to inspect elements and find their attributes.
XPath is case-sensitive for tag names and attribute values.
XPath with attributes helps find elements by their properties.
Use //tagname[@attribute='value'] format.
It makes tests more precise and reliable.
Practice
//input[@type='submit'] select on a web page?Solution
Step 1: Understand XPath syntax
The expression//inputselects all <input> elements anywhere in the document.Step 2: Apply attribute filter
The part[@type='submit']filters those <input> elements to only those whose 'type' attribute equals 'submit'.Final Answer:
All <input> elements with attribute type equal to 'submit' -> Option AQuick Check:
XPath //tag[@attr='value'] selects elements by attribute [OK]
- Confusing tag name with attribute name
- Ignoring the attribute filter
- Assuming it selects elements by text content
Solution
Step 1: Recall correct attribute syntax in XPath
Attributes are selected using@attribute='value'inside square brackets.Step 2: Check each option
//button[@id='loginBtn'] uses@id='loginBtn'which is correct. //button[id='loginBtn'] misses '@'. //button(#id='loginBtn') uses invalid syntax. //button[id="loginBtn"] also misses '@' before the attribute name.Final Answer:
//button[@id='loginBtn'] -> Option AQuick Check:
Use @ before attribute name in XPath filters [OK]
- Omitting @ before attribute name
- Using parentheses instead of square brackets
- Incorrect quoting of attribute values
<input type='checkbox' name='subscribe' checked>What will the XPath
//input[@checked] select?Solution
Step 1: Understand attribute presence in XPath
The XPath[@checked]selects elements where the 'checked' attribute exists, regardless of its value.Step 2: Apply to given HTML
The input element has 'checked' attribute present (even without value), so it matches.Final Answer:
All <input> elements with attribute 'checked' present -> Option DQuick Check:
XPath [@attr] checks attribute presence, not value [OK]
- Assuming attribute must have a value
- Confusing attribute presence with attribute value
- Thinking empty attributes are ignored
//a[@href='https://example.com'] but it fails to find the element. What is the likely error?Solution
Step 1: Verify XPath syntax correctness
The XPath//a[@href='https://example.com']is syntactically correct with proper attribute filter.Step 2: Consider attribute value correctness
If the element is not found, likely the href attribute value in the page differs (e.g., missing trailing slash, different protocol, or relative URL).Final Answer:
The href attribute value is incorrect or incomplete -> Option CQuick Check:
Check exact attribute values when XPath finds no elements [OK]
- Assuming XPath syntax error without checking value
- Using wrong tag name for links
- Believing XPath cannot filter by attributes
<input> elements that have a name attribute starting with 'user' and a type attribute exactly equal to 'text'. Which XPath expression is correct?Solution
Step 1: Identify XPath function for 'starts with'
XPath usesstarts-with(@attr, 'value')to check if attribute starts with a string.Step 2: Combine conditions with 'and'
Both conditions must be true: name starts with 'user' and type equals 'text'. //input[starts-with(@name, 'user') and @type='text'] correctly usesandto combine.Step 3: Evaluate other options
//input[@name^='user' and @type='text'] uses invalid syntax@name^='user'. //input[@name*='user' and @type='text'] uses*=which is not valid XPath. //input[contains(@name, 'user') and @type='text'] usescontains()which matches anywhere, not just start.Final Answer:
//input[starts-with(@name, 'user') and @type='text'] -> Option BQuick Check:
Use starts-with() for prefix match in XPath [OK]
- Using CSS-like ^= or *= operators in XPath
- Confusing contains() with starts-with()
- Not combining multiple conditions properly
Ready to prove it?
Pass the test and this pattern moves unknown
