【问题标题】:Selecting all the values from the Listbox using Selenium C#使用 Selenium C# 从列表框中选择所有值
【发布时间】:2016-04-20 11:19:22
【问题描述】:

我想从列表框中选择所有值。

我尝试了下面的代码,但不幸的是它没有选择列表框中的最后一个值。

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();

列表框控件:-

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

输出:-

我不确定为什么它没有从列表框中选择最后一个值。谁能帮帮我。

【问题讨论】:

    标签: c# selenium listbox


    【解决方案1】:

    我建议使用SelectElement。这是使用 select 的最简单方法。见以下代码:

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

    【讨论】:

    • 我想选择所有元素。 Ur 代码不会维护下拉值的状态..我的意思是它最后只会选择值“Item6”,而不是所有值
    • @Geetha 你试过了吗?
    • 我的错..我没有在列表框中将属性设置为多个..您的代码工作正常
    • @Geetha 没问题。我将告诉您有关多重属性的信息,但您已经找到了。 ^.^
    • 昨天我试过这个逻辑,当时我还没有启用多重属性
    【解决方案2】:

    我注意到你的代码中有这个

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

    注意到您已经选择了除 1 之外的所有元素?

        int totalElementCount = elements.Count - 1;
    

    如果你把它改成

        int totalElementCount = elements.Count;
    

    那么它有效吗? 不幸的是,我无法在我的机器上执行您的代码,但这似乎是合适的。

    如果你这样说会发生什么

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

    或者这可能会有所帮助

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

    我尝试运行您的代码.. 但不幸遇到了太多问题 与硒和火狐... https://stackoverflow.com/a/8188839/1869220

    不能为此改变我的环境。对不起:(

    【讨论】:

    • @Martin - 我也试过那个,它抛出 Index out of bound 异常。因为这些值是由它的索引标识的
    • 那行 act.ClickAndHold(elements[0]).Perform();那是你选择第一个元素的地方吗?
    • 它将选择第一个元素并按住它
    • 如果你这样说会发生什么 act.ClickAndHold(elements[elements.Count]).Perform();或 act.ClickAndHold(elements[elements.Count - 1]).Perform();或者这可能会有所帮助 for(int i = 0; i
    • act.ClickAndHold(elements[elements.Count - 1]).Perform(); - 列表框中没有选择任何内容
    【解决方案3】:

    默认情况下,点击动作发生在元素的左上角。您可能想在最后一个元素上尝试中间。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      相关资源
      最近更新 更多