【问题标题】:How to find Radio button is checked or not in Robot framework如何在机器人框架中检查或未检查单选按钮
【发布时间】:2020-03-17 05:56:42
【问题描述】:

如何找到是否使用机器人框架检查了单选按钮

例如:

<div>
   <label>Male</label> 
      <input name="gender" id="genderM" value="m" />
   <label>Female</label>
      <input name="gender" id="genderF" value="f" checked />
</div>

【问题讨论】:

  • 您可以在 WebElement 中使用 isSelected 方法。

标签: python selenium robotframework


【解决方案1】:

例如:

<div>
       <label>Male</label> 
          <input name="gender" id="genderM" value="male" />
       <label>Female</label>
          <input name="gender" id="genderF" value="female" checked />
</div>

下面的代码是检查并选中复选框

${Status}   Run Keyword And Return Status   Radio Button Should Be Set To    gender    male

如果 ${Status} 为 True,则选中单选按钮男性,否则选中单选按钮女性。根据 ${Status},我们可以使用下面提到的关键字选择所需的单选按钮。

Run Keyword If    '${Status}' == 'False'    select radio button     gender   male
Run Keyword Unless  '${Status}' == 'False'  log     Already it is selected

【讨论】:

    【解决方案2】:

    这对我有用。 HTML 看起来像:

    <input class="jss816" name="visibility" type="radio" value="AdminOnly" checked="">
    <input class="jss816" name="visibility" type="radio" value="Internal">
    

    如果选中此无线电,则此 Robot 命令返回 1:

    get element count  css:input[name='visibility'][value='Internal']:checked
    

    不清楚为什么这不能用 xpath 完成,但我找不到方法!

    【讨论】:

      【解决方案3】:

      检查关键字:

      Element Should Be Enabled | locator 
      

      【讨论】:

      • enabled 表示元素是启用还是禁用?
      【解决方案4】:

      您可以使用 Selenium RF 关键字https://robotframework.org/Selenium2Library/Selenium2Library.html#Radio%20Button%20Should%20Be%20Set%20To

      Radio Button Should Be Set To   group_name, value   
      

      验证单选按钮组 group_name 是否设置为值。 group_name 是单选按钮组的名称。

      【讨论】:

      • 我不想要单选按钮的值。我想测试是否选中了哪个单选按钮属性。我想测试一下单选按钮是否被选中
      【解决方案5】:

      Robots 似乎为此提供了一些内置功能——你可以使用Checkbox Should Be Selected

      Checkbox Should Be Selected | locator | Verifies checkbox locator is selected/checked.
      
      See the Locating elements section for details about the locator syntax.
      

      【讨论】:

      • 仅查看 HTML 时,复选框和单选按钮非常相似——我认为这应该与单选按钮一起使用,但我可能错了。不过值得一试。
      【解决方案6】:

      您可以使用以下代码进行验证

      def is_checked(self, driver, item):
      checked = driver.execute_script(("return document.getElementById('%s').checked") % item)
      return checked
      

      或者你可以使用

      driver.find_element_by_name("< check_box_name >").is_selected()
      

      【讨论】:

        猜你喜欢
        • 2017-09-13
        • 2019-05-29
        • 2021-06-22
        • 2018-08-22
        • 1970-01-01
        • 2021-01-17
        • 2023-03-28
        • 1970-01-01
        • 2017-12-31
        相关资源
        最近更新 更多