【问题标题】:Trying to click radio button option by name/value in Selenium using C#尝试使用 C# 在 Selenium 中按名称/值单击单选按钮选项
【发布时间】:2014-01-24 19:25:51
【问题描述】:

我是 Selenium 的新手,能够从下拉列表中选择一个值,但似乎无法为单选按钮执行此操作。这是我用于下拉列表的代码:

public void SelectValueById(string element, string text) {
    //Get hold of the dropdown box by Name
    IWebElement dropDown = commondriver.FindElement(By.Id(element));

    //Place the drop down into selectElement
    SelectElement clickThisitem = new SelectElement(dropDown);

    //Select the Item from dropdown by Text
    clickThisitem.SelectByText(text);
}

这很好用。我想用单选按钮做同样的事情。我想传入相同的两个参数——单选按钮 id 或 xpath,以及要选择的选项的名称/值——并让函数单击正确的选项。

【问题讨论】:

    标签: c# selenium selenium-webdriver


    【解决方案1】:

    单选按钮不被视为<select> 元素。它是一个具有type='radio' 属性的<input> 元素。检查和点击都足够了。 (有时由于 ajax 绑定等原因点击更多)

    IWebElement radio = commondriver.FindElement(By.Id(element));
    radio.click(); // this will work
    radio.check(); // so will this.
    

    【讨论】:

    • 谢谢你,但这似乎只会点击我传入的一个特定选项。但我希望能够单独指定单选按钮组,以及要选择的特定选项。因为我正在测试的应用程序有不会改变的问题(如“眼睛颜色”)和我希望能够通过 Excel 电子表格更改和传递的其他选项(“蓝色”、“绿色”、“淡褐色” “, ETC。)。我希望这是有道理的。
    • 等等等等,收音机是什么意思?你指的是<select multiple='multiple'>吗?
    • 不,我指的是常规单选按钮——您可以点击的圆点,一次只能选择一个。 (我没有足够的代表来发布图片)。也许我错误地认为单选按钮(或者更确切地说,单选按钮组)是一个值列表,就像选择元素一样。
    • 这不是一个值列表。从语义上讲,单选按钮是几个 <input type='radio' name='my_radio' value='1'> <input type='radio' name='my_radio' value='2'> 我的解决方案就是你想要的。
    猜你喜欢
    • 2016-01-19
    • 2019-08-18
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多