XPath is a way to find elements on a web page by navigating the HTML structure using paths and conditions.
Click to reveal answer
beginner
How do you write an XPath to find an element with a specific attribute value?
Use the syntax: //tagname[@attribute='value']. For example, //input[@id='username'] finds an input element with id 'username'.
Click to reveal answer
beginner
What does this XPath mean? //button[@type='submit']
It finds all button elements on the page that have an attribute type with the value submit.
Click to reveal answer
intermediate
How can you find an element with multiple attribute conditions using XPath?
Use and inside the brackets: //tagname[@attr1='value1' and @attr2='value2']. For example, //input[@type='text' and @name='email'].
Click to reveal answer
beginner
Why is using attributes in XPath locators helpful in Selenium tests?
Attributes help find elements precisely and reliably, even if the page layout changes, making tests more stable.
Click to reveal answer
Which XPath expression finds a div with class 'header'?
A//div[@class='header']
B//div[class='header']
C//div[@id='header']
D//header[@class='div']
✗ Incorrect
The correct syntax uses @ to specify the attribute: //div[@class='header'].
How do you select an input element with name 'password' and type 'password' using XPath?
A//input[@name='password' or @type='password']
B//input[@name='password' and @type='password']
C//input[name='password' and type='password']
D//input[@name='password'][@type='password']
✗ Incorrect
Use and inside one bracket to combine conditions: //input[@name='password' and @type='password'].
What does this XPath select? //a[@href]
AAll <a> elements with any href attribute
BAll <a> elements without href attribute
CAll elements with href attribute
DAll <href> elements
✗ Incorrect
It selects all a tags that have an href attribute, regardless of its value.
Which XPath is correct to find a button with id 'submitBtn'?
A//button[id='submitBtn']
B//button[@id='submitBtn']
C//button[@id=submitBtn]
D//button[@id='submitBtn']/
✗ Incorrect
Attributes must be prefixed with @ and values in quotes: //button[@id='submitBtn'].
Why avoid using XPath with only tag names for locating elements?
AIt is faster
BIt may select many elements, causing unreliable tests
CIt is more readable
DIt works only in Chrome
✗ Incorrect
Using only tag names can select multiple elements, making tests less reliable.
Explain how to write an XPath expression to locate an element by a single attribute.
Think about how you tell Selenium to find an element with a specific attribute.
You got /5 concepts.
Describe how to combine multiple attribute conditions in an XPath locator.
Remember how to say 'and' in XPath conditions.
You got /4 concepts.
Practice
(1/5)
1. What does the XPath expression //input[@type='submit'] select on a web page?
easy
A. All <input> elements with attribute type equal to 'submit'
B. All elements with tag name 'submit'
C. All elements with attribute 'input' and value 'submit'
D. All <submit> elements inside <input> tags
Solution
Step 1: Understand XPath syntax
The expression //input selects 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 A
Quick Check:
XPath //tag[@attr='value'] selects elements by attribute [OK]
Hint: Look for tagname and attribute=value inside square brackets [OK]
Common Mistakes:
Confusing tag name with attribute name
Ignoring the attribute filter
Assuming it selects elements by text content
2. Which of the following is the correct XPath syntax to select a button element with id 'loginBtn'?
easy
A. //button[@id='loginBtn']
B. //button[id='loginBtn']
C. //button(#id='loginBtn')
D. //button[id="loginBtn"]
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 A
Quick Check:
Use @ before attribute name in XPath filters [OK]
Hint: Always prefix attribute with @ in XPath filters [OK]
Common Mistakes:
Omitting @ before attribute name
Using parentheses instead of square brackets
Incorrect quoting of attribute values
3. Given the HTML snippet:
<input type='checkbox' name='subscribe' checked>
What will the XPath //input[@checked] select?
medium
A. No elements, because 'checked' has no value
B. All <input> elements with attribute 'checked' equal to 'true'
C. All <input> elements with attribute 'checked' equal to 'checked'
D. All <input> elements with attribute 'checked' present
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 D
Quick Check:
XPath [@attr] checks attribute presence, not value [OK]
Hint: Use [@attr] to check attribute exists, value not needed [OK]
Common Mistakes:
Assuming attribute must have a value
Confusing attribute presence with attribute value
Thinking empty attributes are ignored
4. You wrote this XPath to find a link: //a[@href='https://example.com'] but it fails to find the element. What is the likely error?
medium
A. The tag name 'a' is wrong, should be 'link'
B. XPath syntax is invalid because of missing @
C. The href attribute value is incorrect or incomplete
D. XPath cannot use attribute filters
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 C
Quick Check:
Check exact attribute values when XPath finds no elements [OK]
Hint: Verify exact attribute values match in XPath and page [OK]
Common Mistakes:
Assuming XPath syntax error without checking value
Using wrong tag name for links
Believing XPath cannot filter by attributes
5. You want to select all <input> elements that have a name attribute starting with 'user' and a type attribute exactly equal to 'text'. Which XPath expression is correct?
hard
A. //input[@name^='user' and @type='text']
B. //input[starts-with(@name, 'user') and @type='text']
C. //input[@name*='user' and @type='text']
D. //input[contains(@name, 'user') and @type='text']
Solution
Step 1: Identify XPath function for 'starts with'
XPath uses starts-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 uses and to 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'] uses contains() which matches anywhere, not just start.
Final Answer:
//input[starts-with(@name, 'user') and @type='text'] -> Option B
Quick Check:
Use starts-with() for prefix match in XPath [OK]
Hint: Use starts-with() for attribute prefix matching in XPath [OK]
Common Mistakes:
Using CSS-like ^= or *= operators in XPath
Confusing contains() with starts-with()
Not combining multiple conditions properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.