使用 Selenium 和 Ruby 选择单选按钮
好吧,我知道这听起来像是一个非常基本的问题,而且我知道有文档解释了如何做到这一点,但是我没有这样做的运气。
假设我使用 Selenium 在 Ruby 中创建驱动程序:
driver = Selenium::WebDriver.for :firefox, :profile => profile
假设我想单击一个链接,这可以使用以下语法正常工作:
driver.find_element(:class => "button").click
但是,如果我想选择某个表中的某个单选按钮,例如:
<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="TESTNAME" VALUE="TESTVALUE2" ></td>
如果我想检查“TESTVALUE2”,我似乎无法这样做。例如下面这一行:
driver.find_element(:name => "TESTNAME").send_keys("TESTVALUE2")
那么我是否缺少一些用于 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="TESTNAME" 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道为什么在这个问题上贴上 Watir 标签,但我将从这个角度做出回应。如果您使用 Watir-Webdriver,它将 webdriver 后端与 Watir API 结合在一起,您将执行类似的操作来加载适当的 gem 并创建 firefox 浏览器对象的实例
然后,一旦您导航到该页面,将单选按钮设置为打开,您会使用
很多使用 Ruby 进行编码的人更喜欢 Watir api,因为它似乎被认为比 Selenium 更“Ruby-esq”(Selenium 更符合 Java-esq)
。详细的watir-webdriver 简介,请参见:http://watirmelon。 com/2010/12/14/watir-webdriver-a-detailed-introduction/
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
Then later once you have navigated to the page, to set a radio button to on, you would use
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
to have the desired effect.