Bird
0
0

What is wrong with this Selenium Java code snippet for selecting a date?

medium📝 Debug Q7 of 15
Selenium Java - Handling Form Elements
What is wrong with this Selenium Java code snippet for selecting a date?
WebElement submitBtn = driver.findElement(By.id("submitBtn"));
driver.findElement(By.xpath("//td[@data-date='2024-06-15']")).click();
submitBtn.click();
The test fails with StaleElementReferenceException on the second click.
AThe XPath locator is incorrect and causes stale element.
BThe date picker reloads the page, invalidating the first element reference.
CsubmitBtn id does not exist on the page.
Dclick() cannot be called twice in a row.
Step-by-Step Solution
Solution:
  1. Step 1: Understand StaleElementReferenceException cause

    This exception occurs when the DOM changes after locating an element.
  2. Step 2: Analyze date picker behavior

    Clicking a date often reloads or updates the page, invalidating previous element references.
  3. Final Answer:

    The date picker reloads the page, invalidating the first element reference. -> Option B
  4. Quick Check:

    Page reload causes stale element error [OK]
Quick Trick: Page reloads cause stale element errors [OK]
Common Mistakes:
  • Blaming XPath locator syntax
  • Assuming submitBtn is missing
  • Thinking multiple clicks cause error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes