Bird
Raised Fist0
Selenium Javatesting~20 mins

XPath with attributes in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
XPath Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate
2:00remaining
Locate element by single attribute
Which XPath expression correctly locates a button element with the attribute id='submitBtn'?
A//button[id='submitBtn']
B//button[@id='submitBtn']
C//button[@id:= 'submitBtn']
D//button[@id='submitBtn']/
Attempts:
2 left
💡 Hint
Remember that attribute selectors in XPath use @ before the attribute name and use = for comparison.
❓ assertion
intermediate
2:00remaining
Verify element text using XPath with attribute
Given the XPath //div[@class='alert'], which assertion correctly verifies the element's text equals 'Success' in Java Selenium?
Selenium Java
WebElement alert = driver.findElement(By.xpath("//div[@class='alert']"));
AassertEquals(alert.getText(), "Success");
BassertTrue(alert.getText() == "Success");
CassertEquals("Success", alert.getText());
DassertTrue(alert.getText().equalsIgnoreCase("Success"));
Attempts:
2 left
💡 Hint
Use assertEquals(expected, actual) for exact string match.
❓ Predict Output
advanced
2:00remaining
Output of XPath with multiple attributes
What is the output of the following Java Selenium code snippet if the page contains exactly one <input type='text' name='username' value='user1'> element?
Selenium Java
WebElement input = driver.findElement(By.xpath("//input[@type='text' and @name='username']"));
System.out.println(input.getAttribute("value"));
Auser1
Bnull
Cusername
DSyntaxError
Attempts:
2 left
💡 Hint
The XPath selects input with type text and name username, then prints its value attribute.
🔧 Debug
advanced
2:00remaining
Identify the error in XPath with attribute contains
What error will the following XPath expression cause in Selenium if used as By.xpath("//a[contains(@href, 'login')"]?
ASyntaxError due to missing closing parenthesis
BNoSuchElementException because no element matches
CInvalidSelectorException due to unbalanced quotes
DNo error, works correctly
Attempts:
2 left
💡 Hint
Check if all parentheses and quotes are properly closed.
❓ framework
expert
3:00remaining
Best practice for locating dynamic elements with attributes
In a Selenium Java test framework, which approach is best to locate a button with a dynamic id attribute that always starts with btn_ and has a fixed data-action='submit' attribute?
AUse XPath: //button[contains(@id, 'btn_') and @data-action='submit']
BUse CSS selector: button[id*='btn_'][data-action='submit']
CUse CSS selector: button[id^='btn_'][data-action='submit']
DUse XPath: //button[starts-with(@id, 'btn_') and @data-action='submit']
Attempts:
2 left
💡 Hint
Consider which selector matches the start of the id attribute exactly and the fixed attribute.

Practice

(1/5)
1. What does the XPath expression //input[@type='submit'] select in 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' equal to 'submit'
D. All <submit> elements inside <input> tags

Solution

  1. Step 1: Understand XPath tag and attribute syntax

    The expression uses //input to select all <input> tags anywhere in the document.
  2. Step 2: Analyze the attribute filter

    The filter [@type='submit'] selects only those <input> elements whose 'type' attribute exactly equals 'submit'.
  3. Final Answer:

    All <input> elements with attribute type equal to 'submit' -> Option A
  4. Quick Check:

    XPath //tag[@attr='value'] selects elements with that attribute value [OK]
Hint: Look for tag name then attribute filter in square brackets [OK]
Common Mistakes:
  • Confusing tag name with attribute name
  • Using wrong attribute syntax like @type=submit without quotes
  • Thinking it selects elements named 'submit'
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

  1. Step 1: Recall correct attribute syntax in XPath

    Attributes are selected using @attributeName='value' inside square brackets.
  2. Step 2: Check each option

    //button[@id='loginBtn'] uses @id='loginBtn' correctly. Options A, B, and D use invalid syntax for attributes.
  3. Final Answer:

    //button[@id='loginBtn'] -> Option D
  4. Quick Check:

    Attributes need '@' before name in XPath [OK]
Hint: Always prefix attribute names with '@' in XPath [OK]
Common Mistakes:
  • Omitting '@' before attribute name
  • Using parentheses or '#' incorrectly
  • Confusing XPath with CSS selector syntax
3. Given the HTML snippet:
<input type='checkbox' name='subscribe' checked>

What will the XPath //input[@checked] select?
medium
A. All <input> elements with attribute 'checked' present
B. All <input> elements with attribute 'checked' equal to 'true'
C. All <input> elements with attribute 'checked' equal to 'checked'
D. No elements, because 'checked' has no value

Solution

  1. Step 1: Understand attribute presence in XPath

    The XPath //input[@checked] selects all <input> elements that have the 'checked' attribute, regardless of its value.
  2. Step 2: Analyze the HTML snippet

    The <input> element has the 'checked' attribute present (without a value), so it matches the XPath.
  3. Final Answer:

    All <input> elements with attribute 'checked' present -> Option A
  4. Quick Check:

    XPath [@attr] checks attribute presence, not value [OK]
Hint: [@attr] means attribute exists, value not needed [OK]
Common Mistakes:
  • Assuming attribute must have a value
  • Confusing attribute presence with value equality
  • Thinking empty attributes are ignored
4. You wrote the XPath //div[@class='container'] but it does not find the element. The element's class attribute is "container main". What is the issue?
medium
A. XPath cannot select elements by class attribute
B. XPath looks for exact match; 'container main' ≠ 'container'
C. The attribute name should be 'className' not 'class'
D. XPath requires double quotes inside single quotes

Solution

  1. Step 1: Understand attribute matching in XPath

    Using @class='container' matches only if the class attribute is exactly 'container'.
  2. Step 2: Analyze the actual class attribute

    The element's class is 'container main', which is not an exact match, so XPath does not select it.
  3. Final Answer:

    XPath looks for exact match; 'container main' ≠ 'container' -> Option B
  4. Quick Check:

    Attribute equality in XPath is exact, no partial match [OK]
Hint: Exact match fails if attribute has multiple values separated by spaces [OK]
Common Mistakes:
  • Assuming XPath matches partial attribute values
  • Using wrong attribute name like 'className'
  • Thinking quotes style causes failure
5. You want to select all <input> elements that have a type attribute equal to 'text' or 'password' using XPath. Which expression is correct?
hard
A. //input[@type='text','password']
B. //input[@type='text' and @type='password']
C. //input[@type='text' or @type='password']
D. //input[@type=('text','password')]

Solution

  1. Step 1: Understand logical operators in XPath

    To select elements matching either condition, use or between attribute tests.
  2. Step 2: Analyze each option

    //input[@type='text' or @type='password'] correctly uses or to select inputs with type 'text' or 'password'. //input[@type='text' and @type='password'] uses and which is impossible for one attribute to have two values simultaneously. Options C and D use invalid syntax.
  3. Final Answer:

    //input[@type='text' or @type='password'] -> Option C
  4. Quick Check:

    Use 'or' to combine attribute conditions in XPath [OK]
Hint: Use 'or' to select elements matching either attribute value [OK]
Common Mistakes:
  • Using 'and' instead of 'or' for multiple attribute values
  • Trying to list values inside one attribute test
  • Incorrect syntax with parentheses or commas
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard