开发者

With selenium getting the order of items in the dropdown or multiselect list

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 value开发者_C百科s 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.)


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']");


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜