Home > Software design >  Getting an Error When I Try To Output What Was Chosen From a Dropdown
Getting an Error When I Try To Output What Was Chosen From a Dropdown

Time:08-12

So I have a method which I am getting a random item from a dropdown menu. I would like to output the item which was chosen so I know which option was chosen should a test fail. Here is my method:

    public static void DropdownSelectRandomOption(IWebElement dropDown)
    {
        dropdown. Click();
        Random rnd = new Random();
        var selectElement = new SelectElement(dropDown);
        int itemCount = selectElement.Options.Count;
        var itemChosen = selectElement.SelectByIndex(rnd.Next(0, itemCount));
        Console.WriteLine($"The item chosen from the dropdown is: {itemChosen}");
        itemChosen.Click();
        dropDown.Click();
        
    }

I am running into an error on the line for var itemChosen it is saying Cannot Assign Void to an implicitly-typed variable what am I doing wrong here?

CodePudding user response:

SelectByIndex doesn't return anything, so there's nothing to assign to itemChosen.

It looks like you just want to select from the Options property:

var itemChosen = selectElement.Options[rnd.Next(0, itemCount)];

It's also likely (though not guaranteed, I don't have a test handy) that the console output won't contain anything useful about itemChosen. You may want to output a specific property instead.

  • Related