0
0
Selenium Javatesting~20 mins

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

Choose your learning style9 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.