What I think is that this involves the lazy loading mechanism, where the loading of the page factory WebElements are deferred till it's required.

Now if this is the cases then what's the need of PageFactory.initElements?

And in the situation where we have a button in the webpage that has id='button' and name='save', and we use page factory to initialize the element to click the button(we use only the id='button' for identifying the element).

After that the button vanishes and a new button comes up having id='button' and name='add'. Can I use the same WebElement used earlier to click on it? Because even though the location strategy is same, the attribute name has changed.


Answering your second question:

No, you can't use the same WebElement. You will get a stale element reference exception because the button on the screen is a different button according to the code. You would need to call refresh() at minimum.

Answering your first question:

The PageFactory model acts as an extension to your PageObject, allowing you to do things like:


instead of the more standard Selenium findElement calls in the page object. This simplifies your page object code because you don't need to define the findElement calls (PageFactory assumes you are running the find by name or ID).

PageFactory.initElements(driver, pageObjectClass) implicitly creates the findElement calls behind the scene.

My suggestion:

Instead of using the id to identify the button, I'd identify it by name. That way, instead of needing to create near-identical code, you could do something like:

// wait until the add button is visible and enabled
// a refresh call may be needed
// then you can call the next line of code
  • Does make the picture a lot clearer for me. ☺️ – Swastik Jun 5 '17 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.