12

Can anyone explain me about Annotation @FindBy in WebDriver?

Where and why it is used?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
mra419
  • 413
  • 4
  • 9
  • 23

4 Answers4

16

It's to assist with the construction of locators when using the Page Factory to support your Page Objects

PageFactory Wiki Page

However I'm discovering that I find it more useful to store your locators as By objects rather than WebElements as they are more flexible and you tend to avoid running into the StaleElementException.

By myLocator = By.id("idOfYourElement")

instead of

@FindBy(id = "idOfYourElement")
WebElement myLocator;

This way you can also use your locators when asserting the absence of an element or use it in the ExpectedConditions helpers.

Edgard Leal
  • 2,592
  • 26
  • 30
el roso
  • 3,586
  • 1
  • 16
  • 12
11

Can I cite API-documentation?

Used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements. Used in conjunction with PageFactory#proxyElement this allows users to quickly and easily create PageObjects.

So, if you use PageObject pattern then you adds this annotation to class members and WebDriver automatically inject appropriate WebElements to it during object initialization (when PageFactory.initElements() called).

I strongly recommend to follow this link and read about PageObject pattern and @FindBy annotations usage with more examples.

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
  • Thanks for your response. So, i have another question...will it be used for elements changing their locators dynamically? – mra419 Jan 27 '12 at 15:42
  • @user1163375 Whats you mean by saying `changing their locators dynamically`? – Slava Semushin Jan 30 '12 at 03:55
  • What my understanding is "@FindBy" Annotation is used when elements ID's are changing dynamically in web application....Correct me if am wrong. – mra419 Jan 30 '12 at 06:44
  • 2
    `@FindBy` is used for getting access to element at web page with known id/name/etc If element's ID created automatically then how WebDriver will know about it? – Slava Semushin Jan 30 '12 at 08:38
  • @SlavaSemushin : could you please update the links, they are broken it seems. Would be of great help. Thanks – Naman Jun 03 '16 at 11:30
3

You can also use Pagefactory, and have something like:

@FindBy(how = How.NAME, using = "logonName")
private WebElement logonNameField;

@FindBy(how = How.NAME, using = "password")
private WebElement passwordField;

Now, as for How., you can have:

  1. CLASS_NAME
  2. CSS
  3. ID
  4. ID_OR_NAME
  5. LINK_TEXT
  6. NAME
  7. PARTIAL_LINK_TEXT
  8. TAG_NAME
  9. XPATH
  10. class

Or you can use your own DOM Search plus Xpath, that would be outside of WebDriver API , but it should work.

Edgard Leal
  • 2,592
  • 26
  • 30
kamal
  • 9,637
  • 30
  • 101
  • 168
0

With the help of PageFactory class, we use annotations @FindBy to find WebElements. We use initElements method to initialize web elements. @FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as attributes.

The @FindBy annotation locates one or more WebElements using a single criterion. For example, to identify all elements that have the same class attribute, we could use the following identification:

@FindBy(how = How.CLASS_NAME, using = "classname")
private List<WebElement> singlecriterion;`enter code here`
The man
  • 129
  • 16