Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting an Error When I Try To Output What Was Chosen From a Dropdown

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading