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?
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?
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