简体   繁体   English

的Watir。 选择多个单选按钮选项

[英]Watir. Select multiple radio button options

The issue I'm dealing with, and unable to solve due to my ignorance, is that I have a page on a browser with different education requirements which each have four radio buttons. 由于我的无知,我正在处理并无法解决的问题是,我在浏览器上有一个页面,该浏览器具有不同的教育要求,每个都有四个单选按钮。 I want to be able to select a certain radio button for each education requirement. 我希望能够为每个教育要求选择一个单选按钮。

The code I have is like this: 我的代码是这样的:

radios = browser.radios
radios.each do |radio|
  radio.input(:value => "very").set
end

However this keep giving me an error message saying: "undefined method 'set' for #Watir::Input:0x103a5d508" 然而,这继续给我一个错误消息说:“未定义的方法'设置'为#Watir ::输入:0x103a5d508”

I did something similar for select_lists where I changed the option of all select_lists on a page to the 2nd option which worked: 我为select_lists做了类似的事情,我将页面上所有select_lists的选项更改为第二个选项,该选项有效:

lists = browser.select_lists 
lists.each do |list|
  list.option(:index, 1).select
end

For my code for the radio buttons I tried using radio.option but it gave me a similar error: "NoMethodError: undefined method `set' for #Watir::Option:0x103a466a0" 对于我的单选按钮代码,我尝试使用radio.option,但它给了我一个类似的错误:“NoMethodError:#Watir :: Option:0x103a466a0的未定义方法`set'”

Problem 问题

The code 编码

radios = browser.radios
radios.each do |radio|
  radio.input(:value => "very").set
end

Says that for each radio button on the page, set the first input element with value "very". 说对于页面上的每个单选按钮,将第一个输入元素设置为值“very”。 This suggests that you are looking for html like: 这表明你正在寻找像:

<radio>
  <input value="very" />
</radio>

This is probably not what the html looks like. 这可能不是html的样子。

Solution

I assume the html you really want to set is like: 我假设你真的要设置的html是这样的:

<input type="radio" value="very" />

To set each radio button with value "very", the code should be: 要设置值为“非常”的每个单选按钮,代码应为:

# Get a collection of all radios with value "very"
radios = browser.radios(:value => "very")

# Iterate through each radio button in the collection and set it
radios.each do |radio|
  radio.set
end

This can be shortened to simply: 这可以简化为:

browser.radios(:value => "very").each(&:set)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM