Bird
Raised Fist0
Selenium Javatesting~20 mins

findElement by xpath in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
XPath Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of findElement with XPath using contains()
What will be the output of the following Selenium Java code snippet when the page contains a button with text 'Submit Form'?
Selenium Java
WebElement button = driver.findElement(By.xpath("//button[contains(text(),'Submit')]");
System.out.println(button.getText());
ASubmit
BEmpty string
CNoSuchElementException
DSubmit Form
Attempts:
2 left
💡 Hint
Remember that contains() matches any substring inside the text node.
❓ assertion
intermediate
2:00remaining
Correct assertion for element found by XPath
Which assertion correctly verifies that an element found by XPath has the expected text 'Login'?
Selenium Java
WebElement loginButton = driver.findElement(By.xpath("//button[@id='login']"));
AassertEquals("Login", loginButton.getText());
BassertTrue(loginButton.getText() == "Login");
CassertFalse(loginButton.getText().equals("Login"));
DassertNull(loginButton.getText());
Attempts:
2 left
💡 Hint
Use the correct assertion method to compare strings in Java.
❓ locator
advanced
2:00remaining
Best XPath locator for a button with dynamic id but fixed text
Given a button with a dynamic id but fixed visible text 'Confirm', which XPath locator is best to find it reliably?
A//button[text()='Confirm']
B//button[@id='btn123']
C//button[contains(@id, 'btn') and text()='Confirm']
D//button[contains(text(), 'Conf')]
Attempts:
2 left
💡 Hint
Focus on stable attributes or text for locating elements.
🔧 Debug
advanced
2:00remaining
Identify the error in XPath usage
What error will occur when running this Selenium Java code? WebElement element = driver.findElement(By.xpath("//div[@class='content']")); String text = element.getText(); System.out.println(text);
Selenium Java
WebElement element = driver.findElement(By.xpath("//div[@class='content']"));
String text = element.getText();
System.out.println(text);
ANullPointerException because element is null
BNoSuchElementException if no div with class 'content' exists
CSyntaxError in XPath expression
DClassCastException when casting WebElement
Attempts:
2 left
💡 Hint
Consider what happens if the element is not found.
❓ framework
expert
2:00remaining
Best practice for waiting for element by XPath before interaction
In Selenium Java, which approach correctly waits up to 10 seconds for an element located by XPath before clicking it?
AThread.sleep(10000); driver.findElement(By.xpath("//button[@id='submit']")).click();
Bdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElement(By.xpath("//button[@id='submit']")).click();
Cnew WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='submit']"))).click();
Ddriver.findElement(By.xpath("//button[@id='submit']")).click();
Attempts:
2 left
💡 Hint
Use explicit waits for better reliability than implicit waits or sleep.

Practice

(1/5)
1. What does the Selenium method findElement(By.xpath("//button[@id='submit']")) do?
easy
A. Finds the first button element with id 'submit' on the page
B. Finds all button elements with id 'submit' on the page
C. Clicks the button with id 'submit' automatically
D. Deletes the button element with id 'submit'

Solution

  1. Step 1: Understand the method purpose

    The method findElement locates a single web element matching the given locator.
  2. Step 2: Analyze the XPath expression

    The XPath //button[@id='submit'] selects the first <button> element with attribute id='submit'.
  3. Final Answer:

    Finds the first button element with id 'submit' on the page -> Option A
  4. Quick Check:

    findElement + XPath = single matching element [OK]
Hint: findElement returns one element matching XPath [OK]
Common Mistakes:
  • Confusing findElement with findElements
  • Thinking it clicks or deletes element
  • Assuming it returns multiple elements
2. Which of the following is the correct syntax to find an element with XPath in Selenium Java?
easy
A. driver.findElement(By.xpath('//input[@name='username']'));
B. driver.findElement(By.xpath=//input[@name='username']);
C. driver.findElement(By.xpath("//input[@name='username']"));
D. driver.findElement(By.xpath("input[@name='username']"));

Solution

  1. Step 1: Check method call syntax

    The correct method call is driver.findElement(By.xpath("xpath_string")) with double quotes around the XPath string.
  2. Step 2: Validate each option

    driver.findElement(By.xpath("//input[@name='username']")); uses correct syntax with double quotes and proper method call. driver.findElement(By.xpath=//input[@name='username']); uses invalid assignment syntax. driver.findElement(By.xpath('//input[@name='username']')); uses single quotes inside double quotes incorrectly. driver.findElement(By.xpath("input[@name='username']")); misses the leading '//' in XPath.
  3. Final Answer:

    driver.findElement(By.xpath("//input[@name='username']")); -> Option C
  4. Quick Check:

    Correct method call with By.xpath and string [OK]
Hint: Use By.xpath("xpath") with double quotes inside parentheses [OK]
Common Mistakes:
  • Using assignment '=' instead of method argument
  • Mixing single and double quotes incorrectly
  • Omitting '//' in XPath expression
3. What will be the result of this code snippet?
WebElement element = driver.findElement(By.xpath("//div[@class='menu']/ul/li[2]"));
System.out.println(element.getText());
Assuming the HTML is:
<div class='menu'>
  <ul>
    <li>Home</li>
    <li>About</li>
    <li>Contact</li>
  </ul>
</div>
medium
A. Home
B. NoSuchElementException
C. Contact
D. About

Solution

  1. Step 1: Understand the XPath expression

    The XPath //div[@class='menu']/ul/li[2] selects the second <li> child inside the <ul> within the div with class 'menu'.
  2. Step 2: Match XPath to HTML content

    The second <li> inside the list is About. So element.getText() will print "About".
  3. Final Answer:

    About -> Option D
  4. Quick Check:

    XPath li[2] = second list item text [OK]
Hint: XPath li[2] selects second list item text [OK]
Common Mistakes:
  • Confusing index starting at 0 instead of 1
  • Assuming XPath returns all matching elements
  • Ignoring the class attribute filter
4. You wrote this code but it throws a NoSuchElementException. What is the likely error?
WebElement button = driver.findElement(By.xpath("//button[text()='Submit']"));
button.click();
The HTML is:
<button id='submitBtn'>Submit</button>
medium
A. The button element is inside an iframe and not switched to
B. The XPath is missing the button's id attribute
C. XPath is case-sensitive and 'Submit' text does not match
D. The button is disabled and cannot be clicked

Solution

  1. Step 1: Check XPath correctness

    The XPath //button[text()='Submit'] matches the button text exactly, which is correct here.
  2. Step 2: Consider common Selenium issues

    If the element is inside an iframe, Selenium cannot find it unless switched to that iframe first. This causes NoSuchElementException.
  3. Final Answer:

    The button element is inside an iframe and not switched to -> Option A
  4. Quick Check:

    NoSuchElementException often means iframe switch missing [OK]
Hint: Check if element is inside iframe before findElement [OK]
Common Mistakes:
  • Assuming XPath text match is case-insensitive
  • Ignoring iframe context
  • Thinking disabled buttons cause NoSuchElementException
5. You want to find an input field that has either name='email' or placeholder='Email'. Which XPath expression should you use with findElement?
hard
A. //input[@name='email' | @placeholder='Email']
B. //input[@name='email' or @placeholder='Email']
C. //input[contains(@name, 'email') or contains(@placeholder, 'Email')]
D. //input[@name='email' and @placeholder='Email']

Solution

  1. Step 1: Understand the requirement

    You want to find an input element that matches either condition: name='email' OR placeholder='Email'.
  2. Step 2: Analyze XPath options

    //input[@name='email' or @placeholder='Email'] uses the correct XPath OR operator or between attribute checks. //input[@name='email' and @placeholder='Email'] uses and which requires both attributes. //input[contains(@name, 'email') or contains(@placeholder, 'Email')] uses contains which is broader but acceptable, but the question asks exact match. //input[@name='email' | @placeholder='Email'] uses invalid XPath syntax with | inside predicate.
  3. Final Answer:

    //input[@name='email' or @placeholder='Email'] -> Option B
  4. Quick Check:

    Use 'or' inside XPath predicate for either condition [OK]
Hint: Use 'or' inside XPath brackets for multiple attribute options [OK]
Common Mistakes:
  • Using 'and' instead of 'or' for either condition
  • Using invalid XPath operators like '|' inside predicates
  • Confusing contains() with exact match
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