Selenium Webdriver and PageFactory initialize List<WebElement> elements
Asked Answered
E

6

7

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. =/

Epithet answered 4/11, 2011 at 10:50 Comment(0)
N
4

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;
    } 
Newcomen answered 17/11, 2011 at 17:40 Comment(0)
S
8

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

Scuttle answered 4/11, 2011 at 23:32 Comment(9)
it turns out that it was removed from the release as it was causing other issues. link :( Upsetting as i updated my pom file with the current release of selenium from the maven repository which is release 2.11Epithet
There is going to be a new release this week. Stay tuned!Scuttle
I'm using the newest library files for Selenium Webdriver .Net bindings, but it's still not possible to use [FindBy(How = How.Id, Using = "id")] public List<IWebElement> elements { get; set; } I get the following error: Message: System.ArgumentException : Object of type 'Castle.Proxies.IWrapsElementProxy_1' cannot be converted to type 'System.Collections.Generic.List`1[OpenQA.Selenium.IWebElement]'.Epithet
@PatrickMagee It seems like you are using FindBy, did you try @FindAllBy?Scuttle
I switched to .Net, there is no [FindAllBy attribute.Epithet
The original question was for Java? You might want to edit the question saying you are looking for a .Net solutionScuttle
Yes, I may update it tomorrow. I think it's silly that the api bindings for each language don't contain the same functionality =/ hopefully someone sorts out the .Net API bindings. You win some you lose some using different languages =/Epithet
Consider posting your question on the webdriver google group as well. May be there is an API for .net that we don't know. Their APIs are pretty standard across all bindings, I would be surprise if one feature is available in Java and the same is missing in .Net or python- groups.google.com/group/webdriverScuttle
What is the status of this? Selenium 2.25.0 doesn't have this class.Enchilada
N
4

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;
    } 
Newcomen answered 17/11, 2011 at 17:40 Comment(0)
A
1

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#

Ahvenanmaa answered 23/11, 2011 at 18:51 Comment(0)
P
0
      @FindBys(@FindBy(xpath="//span[@class='ng-binding']"))

        private List<WebElement> AllData;

        public List<WebElement> getAllData() {
            return AllData;
        }
Prime answered 12/9, 2014 at 7:10 Comment(0)
M
0

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.

Musquash answered 22/12, 2016 at 21:37 Comment(0)
U
0

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.

Unquestioning answered 13/5, 2022 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.