Andy Williams
Andy Williams

Reputation: 907

Selenium Webdriver C# Random SelectByIndex from a Dropdown

I have a WebElement which is a dropdown. If I were to select an index from the dropdown list I would use the following code

SelectElement s = new SelectElement(ddlWebElement)
s.SelectByIndex(12);

What syntax would I used to instead of calling a specific index I wanted to randomly generate the selection each time.

UPDATE I tried this code but now i need to figure out how to narrow down the integer selection

Random r = new Random();
SelectElement s = new SelectElement(ddlChooseStore_Cart);
s.SelectByIndex(r.Next());

Upvotes: 1

Views: 2428

Answers (2)

Mostafiz
Mostafiz

Reputation: 7352

if you want to select value between 0-12 index number then this will select value by random index

SelectElement s = new SelectElement(ddlWebElement)
Random rnd = new Random();
int index = rnd.Next(0, 12);
s.SelectByIndex(index);

Upvotes: 1

sujith karivelil
sujith karivelil

Reputation: 29026

The only thing you need to do is replace the magic number 12 with random number. your code for this will like the following:

Random rnd = new Random(); // this can be global
SelectElement s = new SelectElement(ddlWebElement);
int itemCount= s.Items.Count(); // get the count of elements in ddlWebElement
s.SelectByIndex(rnd.Next(0,itemCount));// will give you random selections

Upvotes: 2

Related Questions