0

I have used both of them but not know where should I use @findBy and where I use driver.findElement()

I heard that @FindBy behaves dynamically whereas driver.findElement() behaves as static. What does this mean?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Neeraj
  • 31
  • 5
  • 1
    Possible duplicate of [Selenium WebDriver @FindBy and findElement() difference](https://stackoverflow.com/questions/35980760/selenium-webdriver-findby-and-findelement-difference) – Grasshopper Mar 15 '18 at 08:24

1 Answers1

0

Both @FindBy and driver.findElement() are different approach towards achieving the same target i.e. to locate element/s through different Locator Strategies.

When using PageFactory we can use the Annotation Type FindBy. The FindBy annotations helps us to remove the boiler-plate code which we generally use in the form of findElement() and findElements() when looking for elements.

As an example:

WebElement element = driver.findElement(By.name("q"));
element.click();

becomes:

element.click();

You can find @Simon Stewart's comments on the same topic within the discussion How to use explicit waits with PageFactory fields and the PageObject pattern

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352