加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-4 – 在水豚中选择单选按钮的主要方法是什么?

发布时间:2020-12-17 04:02:52 所属栏目:百科 来源:网络整理
导读:我正在尝试为自由职业者选择单选按钮,代码如下(当我们在浏览器上检查元素时) label for="registration_payer_type_business"input checked="checked" id="registration_payer_type_business" name="registration[payer_type]" type="radio" value="business"
我正在尝试为自由职业者选择单选按钮,代码如下(当我们在浏览器上检查元素时)

<label for="registration_payer_type_business"><input checked="checked" id="registration_payer_type_business" name="registration[payer_type]" type="radio" value="business">
          Company
          </label>

<label for="registration_payer_type_freelancer"><input id="registration_payer_type_freelancer" name="registration[payer_type]" type="radio" value="freelancer">
          Freelancer
          </label>

我试过了

page.choose("registration_payer_type_freelancer")

这不会给出任何错误,但是当在水豚中保存和打开页面时,仍然没有选择对抗自由职业者的广播框.如果人们可以使用xpath给出示例并选择,我将不胜感激.

解决方法

您最有可能遇到的真正问题是save_and_open_page使用当前属性值而非当前属性值保存HTML.这意味着您选择了一个单选按钮(更改已检查的属性值,而不是属性值,不一定会显示).如果要查看页面的当前状态,最好使用save_and_open_screenshot.下面说的是可以选择单选按钮的方法.

要选择具有Capybara的特定单选按钮,您可以使用id,名称,标签文本和值,如果需要使其唯一(例如名称)

choose('registration_payer_type_freelancer') # id
choose('registration[payer_type]',option: 'freelancer') # name and value to make unique
choose('Freelancer') # label text
choose(option: 'freelancer') # just value if the only radio button with that value

在所有这些情况下,如果页面上的实际单选按钮输入元素不可见(用于样式目的等),而您想要单击可见标签,则可以传递allow_label_click:true

choose('registration_payer_type_freelancer',allow_label_click: true) # find by id and select by clicking the label if input is non-visible

您可以使用的其他选项是通过CSS查找(如果您的默认选择器类型是默认值,则可以忽略:css参数:css)

find(:css,'#registration_payer_type_freelancer').click

您也可以使用XPath查询来定位元素,但是98%的时间它们确实不是必需的(更多的人正确理解CSS,并且查找器的范围通常可以用来获取任何元素),并且有问题需要意识到 – https://github.com/teamcapybara/capybara/blob/master/README.md#beware-the-xpath–trap

find(:xpath,'.//input[@value="freelancer"]').click

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读