0

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.

Aishu
  • 1,310
  • 6
  • 28
  • 52

3 Answers3

1

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);
}
Buaban
  • 5,029
  • 1
  • 17
  • 33
  • I want to select all the elements. Ur code will not maintain the state for the dropdown values..i mean it will select only the value 'Item6' at last, not all the values – Aishu Apr 20 '16 at 05:15
  • @Geetha Did you try it yet? – Buaban Apr 20 '16 at 05:24
  • My bad..I didn't set the property to multiple in the listbox..your code works fine – Aishu Apr 20 '16 at 05:38
  • @Geetha No problem. I'm going to tell you about the multiple property but you've already found it. ^.^ – Buaban Apr 20 '16 at 05:39
  • Yesterday i tried this logic, at that time i haven't enabled the multiple property – Aishu Apr 20 '16 at 05:40
0

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 :(

Community
  • 1
  • 1
CA Martin
  • 307
  • 2
  • 7
  • @Martin - I tried that one as well, It is throwing Index out of bound exception. Becasue the values are identified by its index – Aishu Apr 19 '16 at 17:57
  • What about the line act.ClickAndHold(elements[0]).Perform(); Is that where you select the first element? – CA Martin Apr 19 '16 at 17:58
  • It will select the first element and hold it – Aishu Apr 19 '16 at 18:01
  • 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(); } – CA Martin Apr 19 '16 at 18:04
  • act.ClickAndHold(elements[elements.Count - 1]).Perform(); - nothing is selected in the listbox – Aishu Apr 19 '16 at 18:09
  • act.ClickAndHold(elements[elements.Count]).Perform(); - Index out of bound exception thrown – Aishu Apr 19 '16 at 18:09
  • check revised answer... the loop – CA Martin Apr 19 '16 at 18:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109589/discussion-between-ca-martin-and-geetha). – CA Martin Apr 19 '16 at 18:12
  • @martin - Buaban's answer works fine for me...thanks for your time – Aishu Apr 20 '16 at 05:47
  • @Aishu - no problem glad you got it fixed :) – CA Martin May 21 '16 at 00:28
0

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();
Chuck Brown
  • 353
  • 1
  • 5