使用 selenium 获取下拉列表或多选列表中的项目顺序

发布于 2024-11-06 03:22:51 字数 340 浏览 0 评论 0原文

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

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

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

Using selenium I want to get the order of items or rather list of keys of the multiselect list or dropdown.
Seeing in selenium API i see that we can get the index of the selected items but I want to get the index or keys of all the items. Is it possible? If yet then how?
Or if i can get the list the items index and value that also should be fine.
Or anyway to get all the keys or values one at a time for the listbox/multiselect list?

Actually I want to verify that given items are in the correct order.

(Infact I want to provide a fixture to verify that given items are in order.
Say for example list box contains A,B,C,D,E
and some one calls this API with (new String("A,B,c,D,E"),dropDownLocator) then it should return true otherwise false.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菩提树下叶撕阳。 2024-11-13 03:22:52

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

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

定位器 "//some-xpath-here/@attribute-name" 是标准 XPath 的正确语法,但当说到属性 Selenium 使用标准 XPath!相反,它使用了这个 - 请注意删除了最后的virgule:“//some-xpath-here@attribute-name”(正如@Wesley Wiser在我的问题中最初向我指出的< a href="https://stackoverflow.com/questions/5171205/complications-with-seleniums-getattribute-method">Selenium 的 GetAttribute 方法的复杂性)。可能存在标准 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']");

The answer from @9ikhan is correct for getting attributes but I think it is worth mentioning one caveat with that answer, plus providing a more straightforward solution if, in fact, you are looking for the element content rather than attribute values, as you suggest.

Point 1: Syntax for getting an attribute.

The locator "//some-xpath-here/@attribute-name" is correct syntax for standard XPath but when it comes to attributes Selenium does not use standard XPath! Rather, it uses this--note the removal of the final virgule: "//some-xpath-here@attribute-name" (as originally pointed out to me by @Wesley Wiser in my question Complications with Selenium's GetAttribute method). There may be certain peculiar instances where the standard XPath syntax will work -- such as this example :-) -- but in general be aware that you need to use the Selenium syntax.

Point 2: Getting element content instead of attributes.

Here is my version of the same code. First my revised HTML to clearly delineate attribute values from content:

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

And my code fragment happens to be in C#, but it is virtually identical to the prior Java example. Note that I have shown two variations--one for attributes and one for content, so you can uncomment the one you want to test it.

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

But a much simpler solution exists if you just want content:

// Get element content:
// Returns: cookie, donut, muffin
string[] items = selenium.GetSelectOptions("//select[@id='bakedgoods']");
恋你朝朝暮暮 2024-11-13 03:22:52

如果你的 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 中,你可以将其与现有列表进行比较。此外,这不适用于标签。说 ,无法从中检索 English(US),但此代码适用于任何属性。

if your html is like this

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

then you can use something like this

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

I've used java here, your option elements will be inside in the optionList, which you can compare against your existing list. Also, this won't work for labels. say <option value="en_US">English(U.S)</option>,not able to retrieve English(U.S) from it, but this code will work for any attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文