0

If @FindBy returns no element, do we get error as I saw @FindBy could be used to get 0 to multiple elements.

Scenario: @FindBy(id = "btn") private WebElement searchButton; //allows single element to be referenced

Case 1: If we have no elements having id as "btn", then what would be the result? Case 2: If we have multiple elements having id as "btn",then what would be the result?

Please clarify.

  • 2
    Possible duplicate of [What is the use of Annotation "@FindBy"?](https://stackoverflow.com/questions/9028757/what-is-the-use-of-annotation-findby) – Max Vollmer Sep 17 '18 at 07:42

2 Answers2

0

You won't receive an error when calling PageFactory.initElements, element that wasn't found will be set to null. However you will get NullPointerException when you try to use the element.

Guy
  • 46,488
  • 10
  • 44
  • 88
0

Case 1: If we have no elements having id as "btn", then what would be the result?

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"<invalid name>"}

Case 2: If we have multiple elements having id as "btn",then what would be the result?

Since we have multiple elements, we could not do any actions due to ambiguity.

org.openqa.selenium.WebDriverException: unknown error: cannot focus element
Rob
  • 26,989
  • 16
  • 82
  • 98