PageFactory.initElements() doesn't work for Select objects?

1,250 views
Skip to first unread message

confusa

unread,
May 12, 2011, 6:52:31 PM5/12/11
to webdriver
I use the PageFactory to initialize my WebElements no problem. For the
first time I tried to stick a Select in a page object and use the
standard PageFactory.initElements(driver, x), but the Select is null.
Seems it only works for WebElements and RenderedWebElements maybe? If
that is true, I would think it should also be made to work for selects.

Simon Stewart

unread,
May 16, 2011, 5:22:15 AM5/16/11
to webd...@googlegroups.com
Yes, that is true. Adding support for lists of elements has been a
long standing request. Fortunately, it's pretty easy to modify how the
page factory initializes elements.

Simon

> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>
>

talawahdotnet

unread,
May 17, 2012, 8:15:16 PM5/17/12
to webd...@googlegroups.com
Just wanted to make a quick comment even though this is an old post.  I ran into the same problem but the answer Simon provided threw me off course a bit. It seems to relate to a separate issue which has since been resolved (initializing lists of WebElements as opposed to a single Select list).

At first I used the following approach when attempting to use PageFactory.initElements and FindBy to locate/initialize a select/dropdown element.  

@FindBy(id = "countryList")
Select countries;

But with that approach, you get a null object even after you call PageFactory.initElements.  The correct approach is to use a WebElement for @FindBy and then later instantiate a new Select object passing the initialized web element. e.g.

@FindBy(id = "countryList")
WebElement countries;
.
.
.
Select countryDropdown = new Select(countries);

I think what threw me off as well was the fact that I generated my initial code using selenium IDE then started converting it to page objects without realizing that Select is a helper wraps a WebElement instead of a being a subclass of WebElement .  I wasted quite a bit of time trying do things like cast the webelement and was even looking at writing my own initializer (based on Simon's response) before realizing that all I needed to do was wrap the WebElement with the helper class. *facepalm*


On Monday, May 16, 2011 4:22:15 AM UTC-5, Simon Stewart wrote:
Yes, that is true. Adding support for lists of elements has been a
long standing request. Fortunately, it's pretty easy to modify how the
page factory initializes elements.

Simon

On Thu, May 12, 2011 at 11:52 PM, confusa <> wrote:
> I use the PageFactory to initialize my WebElements no problem. For the
> first time I tried to stick a Select in a page object and use the
> standard PageFactory.initElements(driver, x), but the Select is null.
> Seems it only works for WebElements and RenderedWebElements maybe? If
> that is true, I would think it should also be made to work for selects.
>
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.

> To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

On Thu, May 12, 2011 at 11:52 PM, confusa <> wrote:
> I use the PageFactory to initialize my WebElements no problem. For the
> first time I tried to stick a Select in a page object and use the
> standard PageFactory.initElements(driver, x), but the Select is null.
> Seems it only works for WebElements and RenderedWebElements maybe? If
> that is true, I would think it should also be made to work for selects.
>
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.

> To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

On Thu, May 12, 2011 at 11:52 PM, confusa <> wrote:
> I use the PageFactory to initialize my WebElements no problem. For the
> first time I tried to stick a Select in a page object and use the
> standard PageFactory.initElements(driver, x), but the Select is null.
> Seems it only works for WebElements and RenderedWebElements maybe? If
> that is true, I would think it should also be made to work for selects.
>
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.

> To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

Simon Stewart

unread,
May 18, 2012, 7:08:56 AM5/18/12
to webd...@googlegroups.com
PageFactory.initElements can take a ElementLocatorFactory. That's the
key to being able to also annotate Select elements. I've never coded
it up beyond a proof of concept, but it's easy enough to do.

Simon
>> > webdriver+...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/webdriver?hl=en.
>> >
>> >
>
>
> On Monday, May 16, 2011 4:22:15 AM UTC-5, Simon Stewart wrote:
>>
>> Yes, that is true. Adding support for lists of elements has been a
>> long standing request. Fortunately, it's pretty easy to modify how the
>> page factory initializes elements.
>>
>> Simon
>>
>> On Thu, May 12, 2011 at 11:52 PM, confusa <> wrote:
>> > I use the PageFactory to initialize my WebElements no problem. For the
>> > first time I tried to stick a Select in a page object and use the
>> > standard PageFactory.initElements(driver, x), but the Select is null.
>> > Seems it only works for WebElements and RenderedWebElements maybe? If
>> > that is true, I would think it should also be made to work for selects.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "webdriver" group.
>> > To post to this group, send email to webd...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > webdriver+...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/webdriver?hl=en.
>> >
>> >
>
>
> On Monday, May 16, 2011 4:22:15 AM UTC-5, Simon Stewart wrote:
>>
>> Yes, that is true. Adding support for lists of elements has been a
>> long standing request. Fortunately, it's pretty easy to modify how the
>> page factory initializes elements.
>>
>> Simon
>>
>> On Thu, May 12, 2011 at 11:52 PM, confusa <> wrote:
>> > I use the PageFactory to initialize my WebElements no problem. For the
>> > first time I tried to stick a Select in a page object and use the
>> > standard PageFactory.initElements(driver, x), but the Select is null.
>> > Seems it only works for WebElements and RenderedWebElements maybe? If
>> > that is true, I would think it should also be made to work for selects.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "webdriver" group.
>> > To post to this group, send email to webd...@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > webdriver+...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/webdriver?hl=en.
>> >
>> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "webdriver" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/webdriver/-/faWLWOA550YJ.
>
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to
> webdriver+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages