简体   繁体   English

当我尝试 Output 时出现错误 从下拉列表中选择了什么

[英]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.

相关问题 当我尝试从 ImageSource 转换为 BitmapImage 时,为什么会出现运行时错误? - Why am I getting a runtime error when I try to convert from an ImageSource to a BitmapImage? 当我尝试从 Unity StandardAssets 脚本访问“EnginePower”时出现错误 - I'm getting an error when I try to access `EnginePower` from Unity StandardAssets script 当我尝试更新表时从数据库中获取错误消息 - Getting error message from data base when I try to update the table 当我尝试使用DotNetOpenAuth访问其API时,从LinkedIn获得HTTP 400错误 - Getting a HTTP 400 Error from LinkedIn when I try to access its API with DotNetOpenAuth 当我尝试通过JS访问ViewData时遇到编译错误 - Getting a compilation error when I try and access ViewData through JS 尝试更新用户时收到无效的电子邮件错误? - Getting invalid email error when I try to update a user? 当我尝试更改 TextBlock 文本时出错 - Getting error when I try to change TextBlock text 尝试在Windows 2000上安装程序时出现错误 - Getting error when I to try to install my program on Windows 2000 尝试 output 时获取 null 值 - Getting a null value when try to output 尝试从库向主项目添加文件时出现错误? - Getting error when try to add a file from library to main project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM