Patrick Magee
Patrick Magee

Reputation: 2989

Selenium Webdriver and PageFactory initialize List<WebElement> elements

I have searched the Selenium Webdriver APi docs hosted on google code. Currently using the PageFactory to initlize my Page objects, but having issue initilizing a list of WebElement.

What I need is a way to initialize a list of elements, ideally a list of drop-down select boxes.

I have looked at the API references to @Findsby and @ByChained but still can not figure out the best way to initlize a list of dropdown select boxes. I COULD have a seperate WebElement for each one and grab the ID but I would like to initlize a list of List selects

Currently I use the following:

public class PageObject {

        @FindBy(id="element_id")
        private WebElement element;

        public getElement() {
          return element;
        }
}

Is there some way I can use something similar to the following that I seek:

public class PageObject {   

    @FindBys(className="selectItmes")
    private List<WebElement> selects;

    public List<WebElement> getSelects() {
      return selects;
    }  
}

Or must I use a single Web Element for each element? :(

Update

Anyone know how to use the PageFactory and initlize a List elements; using the FindsBy annotation. I can't find any way of doing this yet there are google issues on the selenium google docs site saying this has been fixed in the Java api bindings and in version 2.12 as it was mistaken disabled in 2.11.... I still can't initialize a list. =/

Upvotes: 7

Views: 45640

Answers (6)

Grzech
Grzech

Reputation: 99

I know this is an oldie, but lost a lot of time with similiar issue. At my end problem was that I never really initialized the list. So this did not worked:

    @FindBy(css = .randomlocator)
    private List<WebElement> list;

but this worked:

    @FindBy(css = .randomlocator)
    private List<WebElement> list= new ArrayList<>();

Maybe it will help someone.

Upvotes: 0

Jason Smiley
Jason Smiley

Reputation: 1

I solve this problem like So:

@FindBy(id="element_id")
public List<WebElement> selects;

You now have a list of all the web elements with that ID.

Then, you just grab the element out of the list like you would any other PageFactory WebElement list.

Upvotes: 0

Pritish Panda
Pritish Panda

Reputation: 1

      @FindBys(@FindBy(xpath="//span[@class='ng-binding']"))

        private List<WebElement> AllData;

        public List<WebElement> getAllData() {
            return AllData;
        }

Upvotes: 0

CBRRacer
CBRRacer

Reputation: 4659

You can find the select options fairly easily all you have to do is use the Webdriver.Support dll reference. This gives you access to the SelectElement class. Here's a quick example:

IWebElement element = driver.FindElement(By.TagName("select"));

SelectElement select = new SelectElement(element);
int options = element.FindElements(By.TagName("option")).Count();
select.SelectByIndex(new Random().Next(1, options - 1));

The above code finds the select element, get's a count of the options in that select element and then chooses one at random.

The code may be slightly different because my code is written in C#

Upvotes: 1

Dmitry
Dmitry

Reputation: 64

Here is standard solution what I do in our test framework, until @FindAllBy doesn't work in Selenium library:

private List<WebElement> selects;

public List<WebElement> getSelects() {
      selects = getDriver().findElements(By.xpath("..."));
      return selects;
    } 

Upvotes: 4

nilesh
nilesh

Reputation: 14279

This feature has been recently added in Selenium 2.0. Check this issue. It is fixed now.

From the documents, you could do something like,

@FindAllBy(className="selectItmes") 
List<WebElement> selects;

If you are interested in the code, check this out

Upvotes: 8

Related Questions