brian_kratsch
11 years agoContributor
Selecting unique value from drop down list with duplicates
Background: Keyword Test user, very little coded experience but I can usually follow examples
Issue:
I have run into a HTML Select object (ObjectType Select) that has duplicate values in the list. The Select contains 2 optgroups and in the first optgroup it contains the same value as the 2nd opt group.
I would typically use object.ClickItem using keyword tests to select the item. However if I try this it will click the first option that matches in the list when I really want the option from the 2nd optgroup.
Can anyone provide an example on how to solve this issue as I can only assume it is a common issue.
I have included a screen shot.
Issue:
I have run into a HTML Select object (ObjectType Select) that has duplicate values in the list. The Select contains 2 optgroups and in the first optgroup it contains the same value as the 2nd opt group.
I would typically use object.ClickItem using keyword tests to select the item. However if I try this it will click the first option that matches in the list when I really want the option from the 2nd optgroup.
Can anyone provide an example on how to solve this issue as I can only assume it is a common issue.
I have included a screen shot.
- Hi Brian,
You can also use a custom ClickItem function to click the Nth matching item. For example (assuming your project uses JScript):
/*
Selects the Nth duplicate item from the combo box.
Parameters:
ComboBox - the combo box object.
Label - the item text.
Index - the index (from 1) of the duplicate item to select.
*/
function ClickItem(ComboBox, Label, Index)
{
var ind = 1;
for (var i = 0; i < ComboBox.wItemCount; i++)
{
if (ComboBox.wItem(i) == Label)
{
if (ind == Index)
{
ComboBox.ClickItem(i);
return;
}
else
ind++;
}
}
}
In your keyword test, use the Run Script Routine operation to call this ClickItem function with the following parameters:
ComboBox: Aliases.browser.<page_name_and_name_of_the_select_element>
Label: item text
Index: 2