Selecting a Radio Button Using Selenium and Ruby
Ok, I know this sounds like quite a rudimentary question, and I know there are docs explaining how to do this, however I have not had any luck in doing so.
So say I create my driver in Ruby using Selenium:
driver = Selenium::WebDriver.for :firefox, :profile => profile
Say I want to click a link, this works just fine with the following syntax:
driver.find_element(:class => "button").click
However, if I want to select some radio button that is in some table like:
<td valign="middle"><input type="radio" NAME="TESTNAME" VALUE="TESTVALUE1" ></td>
<td valign="middle"><input type="radio" NAME="TESTNAME" VALUE="TESTVALUE2" ></td>
<td valign="middle"><input type="radio" NAME="TE开发者_如何转开发STNAME" VALUE="TESTVALUE2" ></td>
If I want to check "TESTVALUE2", I cannot seem to do so. For instance the following line:
driver.find_element(:name => "TESTNAME").send_keys("TESTVALUE2")
So is there some method used for Ruby that I am missing? Can someone provide an example of how to go about doing this? Thanks again.
Not sure why the Watir tag is on this question, but I'm going to respond from that perspective. If you were using Watir-Webdriver, which combines the webdriver back-end with the Watir API you would do something like this to load the proper gems and create an instance of a firefox browser object
require "rubygems"
require "watir-webdriver"
require "watir-webdriver/extensions/wait"
browser = Watir::Browser.new :firefox
Then later once you have navigated to the page, to set a radio button to on, you would use
browser.radio(:value, 'TESTVALUE2').set
A lot of people coding in Ruby prefer the Watir api since it seems to be considered to be a lot more 'Ruby-esq' than Selenium (which is more Java-esq)
For a more detailed introduction to watir-webdriver, see: http://watirmelon.com/2010/12/14/watir-webdriver-a-detailed-introduction/
I'm not a selenium user myself, but I would expect something along the lines of
driver.find_element(:name => "TESTNAME", :value => "TESTVALUE2").check
to have the desired effect.
driver.find_element(:id, 'TEST_ID').click
精彩评论