【问题标题】:With selenium getting the order of items in the dropdown or multiselect list使用 selenium 获取下拉列表或多选列表中的项目顺序
【发布时间】:2011-08-21 18:13:21
【问题描述】:

使用 selenium 我想获取项目的顺序,或者更确切地说是多选列表或下拉列表的键列表。 在 selenium API 中看到我们可以获取所选项目的索引,但我想获取所有项目的索引或键。可能吗?如果还那么怎么办? 或者,如果我可以获得列表中的项目索引和值,那也应该没问题。 或者无论如何要为列表框/多选列表一次获取所有键或值?

实际上我想验证给定项目的顺序是否正确。

(事实上我想提供一个夹具来验证给定的项目是否有序。 比如说列表框包含 A,B,C,D,E 有人用 (new String("A,B,c,D,E"),dropDownLocator) 调用这个 API,那么它应该返回 true,否则返回 false。)

【问题讨论】:

    标签: selenium drop-down-menu multi-select fixture


    【解决方案1】:

    @9ikhan 的答案对于获取属性是正确的,但我认为值得一提的是该答案的一个警告,如果实际上您正在寻找元素内容而不是属性值,则提供更直接的解决方案,如你建议。

    第 1 点:获取属性的语法。

    定位器"//some-xpath-here/@attribute-name" 是标准XPath 的正确语法,但当涉及到属性时 Selenium 使用标准XPath!相反,它使用了这个——注意删除了最后的 virgule:"//some-xpath-here@attribute-name"(正如@Wesley Wiser 在我的问题Complications with Selenium's GetAttribute method 中最初向我指出的那样)。标准 XPath 语法可能会在某些特殊情况下起作用——例如这个例子 :-)——但通常请注意,您需要使用 Selenium 语法。

    第 2 点:获取元素内容而不是属性。

    这是我的相同代码版本。首先,我修改后的 HTML 以清楚地描述内容中的属性值:

    <html>
      <body>
        <select id ="bakedgoods">
         <option value="1">cookie</option>
         <option value="2">donut</option>
         <option value="3">muffin</option>
        </select>
      </body>
    </html>
    

    我的代码片段恰好是在 C# 中,但它实际上与前面的 Java 示例相同。请注意,我展示了两种变体——一种用于属性,一种用于内容,因此您可以取消注释要测试的一种。

    var optionCount = (int) selenium.GetXpathCount("//select[@id='bakedgoods']/option");
    var optionList = new List<String>();
    for (int i = 1; i <= optionCount; i++)
    {
        // Get element content:
        // Returns: cookie, donut, muffin
        String option = selenium.GetText("//select[@id='bakedgoods']/option[" + i + "]");
    
        // Get attributes:
        // Returns: 1, 2, 3
        //String option = selenium.GetAttribute("//select[@id='bakedgoods']/option[" + i + "]@value");
        optionList.Add(option);
    }
    

    但如果您只想要内容,则存在一个更简单的解决方案:

    // Get element content:
    // Returns: cookie, donut, muffin
    string[] items = selenium.GetSelectOptions("//select[@id='bakedgoods']");
    

    【讨论】:

      【解决方案2】:

      如果你的html是这样的

      <select id ="lang">
       <option value="en_US">en_US</option>
       <option value="en_GB">en_GB</option>
       <option value="en_IE">en_IE</option></select>
      

      那么你可以使用这样的东西

      int optionCount = selenium.getXpathCount("//select[@id='lang']/option").intValue();
              ArrayList<String> optionList = new ArrayList<String>();
              for (int i = 1; i <= optionCount; i++) {
                  String option = selenium.getAttribute("//select[@id='lang']/option["+i+"]/@value");
                  optionList.add(option);
              }
      

      我在这里使用了 java,您的选项元素将在 optionList 中,您可以将其与现有列表进行比较。此外,这不适用于标签。说&lt;option value="en_US"&gt;English(U.S)&lt;/option&gt;,无法从中检索English(U.S),但此代码适用于任何属性。

      【讨论】:

      • 谢谢 9ikhan。有没有办法获得标签?我的意思是英语(美国)?而不是价值观?
      • 我猜这个标签将是 innerHTML,所以使用 xpath 是不可能的,但是 selenium.getText(domId of the dropdown) 应该将其中的所有值作为字符串返回,你可以拆分并添加到列表中。 innerHTML 是对象模型的一部分,因此不能与 xpath 一起使用。
      • 好吧,我现在使用 Selenium 公开的 getOptions() API 将选项作为数组获取。
      猜你喜欢
      • 2020-08-02
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多