[英]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.
我想 output 选择的项目,所以我知道如果测试失败,选择了哪个选项。 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?我在
var itemChosen
的行上遇到了一个错误,它说Cannot Assign Void to an implicitly-typed variable
我在这里做错了什么?
SelectByIndex
doesn't return anything , so there's nothing to assign to itemChosen
. SelectByIndex
不返回任何东西,因此没有什么可以分配给itemChosen
。
It looks like you just want to select from the Options
property :看起来您只想从
Options
属性中获取 select :
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
.控制台 output 也可能不包含有关
itemChosen
的任何有用信息(尽管不能保证,但我没有手边的测试)。 You may want to output a specific property instead.您可能想要 output 一个特定的属性来代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.