We currently have a big selenium junit project which uses the page object model. We make use of the @FindBy
annotation to declare our WebElements. Now when we run the tests, we randomly get the NoSuchelementException
, which means the page might not have finished loading. We don't want to use an implicit wait because that won't provide a complete solution (an element might be present in the DOM but not interactable yet). An explicit wait might solve this problem. However, how do we use it in this page model architecture without having to take away the @FindBy
annotation? This might have been asked before, but I don't seem to find any solutions yet.

- 225,310
- 48
- 427
- 736

- 41
- 1
- 3
-
Sorry to be obtuse, but why do you have to take away the `@FindBy` annotation? – Vince Bowdren Apr 09 '15 at 12:14
1 Answers
There's a 4 ways I've see you can do this. None of them are fun, although, they get better as you go along (and more complicated to program)
Copy your selector into an explicit wait
Put your
@FindBy
on aList<WebElement>
and wait until the size is not 0.Create a function that accepts a WebElement and calls
isDisplayed()
(or some other non-action function) until it doesn't throw an exceptionA final solution you could implement is to use the Decorator pattern around WebElement. This means that you will need to create your own
FieldDecorator
, and then use that decorator when you initialize your decoratedWebElements
. The decorated webelement would have a exists() function that would basically be a try/catch aroundNoSuchElementExceptions
.
If you are confused about any of these solutions, I can provide code for them, but I think it really good practice to learn how WebElementWait
and Page Objects really work (by implementing your chosen solution), so I won't post it now.

- 7,648
- 5
- 37
- 56
-
-
@robx in both cases I would have a timeout and throw and exception – Nathan Merrill Sep 28 '16 at 01:42
-
Ok, anyway, I realize this is an old question. Perhaps you should state your solutions a bit more obvious. What was given as an answer is a vague bug waiting to happen. Considerations for future answers. – robx Sep 28 '16 at 07:01