使用 Selenium 从 HTML Select(下拉)元素中检索值和文本

Retrieve Values and Text from HTML Select (drop-down) Element with Selenium

为了测试我们的应用程序,我需要对照 Excel 工作簿中的参考选择列表模型检查下拉菜单(Select 元素)options。我们需要检查出现在页面上并由用户选择的 Text 和返回给应用程序的 Value

示例:

<select title="Do you have the right to work in Australia? Required
    <option value="" selected="">No Selection</option>
    <option value="4846">No</option>
    <option value="4845">Yes</option>
</select>

这些值被发送到其他系统,所以它们必须是准确的。所以我们想对照 Excel 中的参考工作簿检查它们。这是我在处理 SelectElement 对象时返回到应用程序的 "values"。

IWebElement pickList = session.FindFieldByLabelText(pickListLabel, tagName: "select");
var selectElement = new SelectElement(pickList);
foreach (var option in selectElement.Options)
            { ... }

...什么? option.Text 可用,但在调试器和其他地方我找不到 .Value。感谢您的帮助。

要获取 IWebElement 值或任何其他属性,您需要使用 GetAttribute() 方法

foreach (var option in selectElement.Options)
{
    string value = option.GetAttribute("value");
}