Aishu
Aishu

Reputation: 1320

Selecting all the values from the Listbox using Selenium C#

I want to select all the values from the listbox.

I tried the below code, But unfortunately it is not selecting the last value in the listbox.

IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://localhost:1479/WebPage.aspx");
            IWebElement dropdownElement = driver.FindElement(By.Id("ListBox1"));
            List<IWebElement> elements = dropdownElement.FindElements(By.TagName("option")).ToList();
            int totalElementCount = elements.Count - 1;
            Actions act = new Actions(driver);
            act.ClickAndHold(elements[0]).Perform();
            act.MoveToElement(elements[totalElementCount]).Release().Perform();

ListBox Control:-

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
        <asp:ListItem>Item1</asp:ListItem>
        <asp:ListItem>Item2</asp:ListItem>
        <asp:ListItem>Item3</asp:ListItem>
        <asp:ListItem>Item4</asp:ListItem>
        <asp:ListItem>Item5</asp:ListItem>
        <asp:ListItem>Item6</asp:ListItem>
        </asp:ListBox>

Output:-

enter image description here

I'm not sure why it is not selecting the last value from the Listbox. Can anyone help me.

Upvotes: 0

Views: 3461

Answers (3)

Buaban
Buaban

Reputation: 5137

I recommend using SelectElement. It is the easiest way to work with select. See code below:

SelectElement select = new SelectElement(d.FindElement(By.Id("ListBox1")));
for (int i=0; i<select.Options.Count;i++)
{
    select.SelectByIndex(i);
}

Upvotes: 1

Chuck Brown
Chuck Brown

Reputation: 363

By default click actions happen in the top left corner of the element. You may want to try the middle on the last element.

    Integer iBottom = elements[totalElementCount].getSize().height;
    Integer iRight = elements[totalElementCount].getSize().width;
    actions.moveToElement(elements[totalElementCount], iRight/2, iBottom/2).release.perform();

Upvotes: 0

CA Martin
CA Martin

Reputation: 367

I noticed that you have this in your code

    int totalElementCount = elements.Count - 1;
    Actions act = new Actions(driver);
    act.ClickAndHold(elements[0]).Perform();
    act.MoveToElement(elements[totalElementCount]).Release().Perform();

Notice that you have selected all elements except 1?

    int totalElementCount = elements.Count - 1;

If you change that to

    int totalElementCount = elements.Count;

Does it work then? Unfortunately I don't have the ability to execute your code on my machine(S) but it seems appropriate.

what happens if you say this instead

    act.ClickAndHold(elements[elements.Count]).Perform();
    or
    act.ClickAndHold(elements[elements.Count - 1]).Perform();

or possibly this will help

        for(int i = 0; i < elements.Count; i++) 
        {
               act.ClickAndHold(elements[i]).Perform();
        }

I tried to get run your code.. but unfortunately ran into too many issues with Selenium and firefox... https://stackoverflow.com/a/8188839/1869220

Can't afford to change my environment for this. Sorry :(

Upvotes: 0

Related Questions