Bird
Raised Fist0
Selenium Javatesting~20 mins

@FindBy annotations in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FindBy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ locator
intermediate
2:00remaining
Identify the correct @FindBy annotation for locating a button by its exact text
Which @FindBy annotation correctly locates a button element with the exact visible text 'Submit'?
A@FindBy(css = "button:contains('Submit')")
B@FindBy(name = "Submit")
C@FindBy(id = "Submit")
D@FindBy(xpath = "//button[text()='Submit']")
Attempts:
2 left
💡 Hint
Remember that CSS selectors do not support :contains pseudo-class in Selenium.
❓ assertion
intermediate
2:00remaining
Determine the assertion outcome for a missing element located by @FindBy
Given the following code snippet, what will happen when the test tries to access the element if it is not present on the page?
Selenium Java
public class LoginPage {
  @FindBy(id = "username")
  private WebElement usernameInput;

  public void enterUsername(String username) {
    usernameInput.sendKeys(username);
  }
}
ANoSuchElementException is thrown when enterUsername is called
BNullPointerException is thrown when enterUsername is called
CThe test passes silently without error
DTimeoutException is thrown immediately when the page loads
Attempts:
2 left
💡 Hint
Think about when Selenium tries to find the element and what happens if it is missing.
❓ Predict Output
advanced
2:00remaining
What is the output of this Selenium Page Object initialization?
Consider the following Java Selenium code snippet. What will be the value of the 'submitButton' variable after initialization?
Selenium Java
public class FormPage {
  @FindBy(css = "button.submit")
  private WebElement submitButton;

  public FormPage(WebDriver driver) {
    PageFactory.initElements(driver, this);
  }

  public WebElement getSubmitButton() {
    return submitButton;
  }
}

// In test method:
FormPage page = new FormPage(driver);
WebElement btn = page.getSubmitButton();
System.out.println(btn == null);
Afalse
Btrue
CNoSuchElementException at initialization
DCompilation error
Attempts:
2 left
💡 Hint
PageFactory creates proxies, so the variable is not null immediately after initElements.
🔧 Debug
advanced
2:00remaining
Find the error in this @FindBy annotation usage
What is wrong with the following @FindBy annotation that causes a runtime error?
Selenium Java
public class DashboardPage {
  @FindBy(xpath = "//div[@class='menu'][1]")
  private WebElement firstMenu;

  @FindBy(css = "div.menu:nth-child(1)")
  private WebElement firstMenuCss;
}
AThe XPath expression selects the first div.menu correctly; no error here
BThe CSS selector 'div.menu:nth-child(1)' does not select the first div.menu as expected
CBoth selectors cause NoSuchElementException at runtime
DThe @FindBy annotation requires 'how' and 'using' parameters, so both are invalid
Attempts:
2 left
💡 Hint
Check how CSS :nth-child works compared to XPath indexing.
❓ framework
expert
3:00remaining
Best practice for using @FindBy with dynamic elements in Selenium
In a Selenium test framework, which approach best handles locating elements that may appear or change dynamically during test execution?
AUse @FindBy with PageFactory and rely on implicit waits to handle dynamic elements
BAvoid @FindBy and locate elements inside test methods with explicit waits each time
CUse @FindBy with PageFactory and add explicit waits before interacting with elements
DUse @FindBy with static locators only and refresh the page to reload elements
Attempts:
2 left
💡 Hint
Think about combining @FindBy with waits to handle timing issues.

Practice

(1/5)
1. What is the main purpose of the @FindBy annotation in Selenium Java?
easy
A. To execute JavaScript code on the web page
B. To locate web elements on a page using different strategies
C. To start the Selenium WebDriver session
D. To take screenshots during test execution

Solution

  1. Step 1: Understand the role of @FindBy

    The @FindBy annotation is used to specify how to find a web element on a page, such as by id, name, xpath, etc.
  2. Step 2: Compare with other options

    Options B, C, and D describe other Selenium features but not element locating.
  3. Final Answer:

    To locate web elements on a page using different strategies -> Option B
  4. Quick Check:

    @FindBy locates elements = A [OK]
Hint: Remember: @FindBy is for locating elements only [OK]
Common Mistakes:
  • Confusing @FindBy with WebDriver commands
  • Thinking @FindBy executes scripts
  • Assuming @FindBy starts browser sessions
2. Which of the following is the correct syntax to locate a web element by its ID using @FindBy annotation?
easy
A. @FindBy(id = "submitBtn")
B. @FindBy(name = submitBtn)
C. @FindBy(xpath = submitBtn)
D. @FindBy(css = #submitBtn)

Solution

  1. Step 1: Check correct attribute usage

    The @FindBy annotation requires the attribute name and value as a string. For ID, it is id = "value".
  2. Step 2: Validate each option

    @FindBy(id = "submitBtn") correctly uses id = "submitBtn". @FindBy(name = submitBtn) misses quotes. @FindBy(xpath = submitBtn) uses xpath but without quotes. @FindBy(css = #submitBtn) uses CSS selector but with incorrect syntax (should be css = "#submitBtn").
  3. Final Answer:

    @FindBy(id = "submitBtn") -> Option A
  4. Quick Check:

    Correct syntax uses id with quotes = B [OK]
Hint: Use quotes around locator values in @FindBy [OK]
Common Mistakes:
  • Omitting quotes around locator values
  • Mixing locator types incorrectly
  • Using invalid syntax for CSS selectors
3. Given the following code snippet, what will be the output when button.isDisplayed() is called?
public class LoginPage {
  @FindBy(name = "loginBtn")
  WebElement button;

  public LoginPage(WebDriver driver) {
    PageFactory.initElements(driver, this);
  }
}

// In test method:
LoginPage page = new LoginPage(driver);
boolean visible = page.button.isDisplayed();
System.out.println(visible);
medium
A. Throws NullPointerException because button is not initialized
B. false always because @FindBy does not locate elements
C. true if the element with name 'loginBtn' is visible on the page
D. Compilation error due to missing @FindBy import

Solution

  1. Step 1: Understand PageFactory initialization

    The constructor calls PageFactory.initElements(driver, this), which initializes all @FindBy annotated elements, including button.
  2. Step 2: Check behavior of isDisplayed()

    If the element with name 'loginBtn' exists and is visible, isDisplayed() returns true; otherwise false. No NullPointerException occurs because initialization is done.
  3. Final Answer:

    true if the element with name 'loginBtn' is visible on the page -> Option C
  4. Quick Check:

    PageFactory initializes elements = true if visible [OK]
Hint: PageFactory.initElements must be called to avoid null elements [OK]
Common Mistakes:
  • Assuming elements are null without initialization
  • Confusing isDisplayed() with element presence
  • Ignoring the need to call PageFactory.initElements
4. Identify the error in the following code snippet using @FindBy:
public class HomePage {
  @FindBy(xpath = "//button[@id='start']")
  WebElement startButton;

  public HomePage(WebDriver driver) {
    // Missing initialization
  }
}
medium
A. Missing call to PageFactory.initElements to initialize elements
B. Incorrect XPath syntax in @FindBy annotation
C. WebElement should be declared as private
D. startButton should be initialized manually with driver.findElement

Solution

  1. Step 1: Check element initialization

    The constructor does not call PageFactory.initElements(driver, this), so startButton remains null.
  2. Step 2: Validate other options

    XPath syntax is correct. WebElement visibility (private/public) is optional. Manual initialization is not needed if PageFactory is used.
  3. Final Answer:

    Missing call to PageFactory.initElements to initialize elements -> Option A
  4. Quick Check:

    PageFactory.initElements missing = D [OK]
Hint: Always call PageFactory.initElements in constructor [OK]
Common Mistakes:
  • Forgetting to initialize elements with PageFactory
  • Assuming XPath syntax errors without checking
  • Thinking manual findElement is required with @FindBy
5. You want to locate multiple elements with the same class name using @FindBy. Which declaration is correct to store all matching elements in a list?
public class ProductsPage {
  // Which line is correct?
}
hard
A. @FindBy(css = ".product-item") WebElement products;
B. @FindBy(className = "product-item") WebElement products;
C. @FindBy(xpath = "//div[@class='product-item']") WebElement products;
D. @FindBy(className = "product-item") List<WebElement> products;

Solution

  1. Step 1: Understand locating multiple elements

    To store multiple elements, the field must be a List<WebElement>. Using @FindBy with className locator is valid.
  2. Step 2: Check each option's type and locator

    @FindBy(className = "product-item") List<WebElement> products; uses List<WebElement> with correct locator. Options A, B, and D use single WebElement, which can only hold one element.
  3. Final Answer:

    @FindBy(className = "product-item") List<WebElement> products; -> Option D
  4. Quick Check:

    Multiple elements need List<WebElement> = C [OK]
Hint: Use List<WebElement> for multiple elements with @FindBy [OK]
Common Mistakes:
  • Declaring multiple elements as single WebElement
  • Using wrong locator type for multiple elements
  • Forgetting to import java.util.List
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