使用 Selenium 和 Ruby 选择单选按钮

发布于 2024-11-19 09:21:11 字数 856 浏览 7 评论 0原文

好吧,我知道这听起来像是一个非常基本的问题,而且我知道有文档解释了如何做到这一点,但是我没有这样做的运气。

假设我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-11-26 09:21:12

不知道为什么在这个问题上贴上 Watir 标签,但我将从这个角度做出回应。如果您使用 Watir-Webdriver,它将 webdriver 后端与 Watir API 结合在一起,您将执行类似的操作来加载适当的 gem 并创建 firefox 浏览器对象的实例

require "rubygems"
require "watir-webdriver"
require "watir-webdriver/extensions/wait"

browser = Watir::Browser.new :firefox

然后,一旦您导航到该页面,将单选按钮设置为打开,您会使用

browser.radio(:value, 'TESTVALUE2').set

很多使用 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

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/

酒几许 2024-11-26 09:21:12

我自己不是硒用户,但我希望类似的东西

driver.find_element(:name => "TESTNAME", :value => "TESTVALUE2").check

能够达到预期的效果。

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.

萌能量女王 2024-11-26 09:21:12
driver.find_element(:id, 'TEST_ID').click 
driver.find_element(:id, 'TEST_ID').click 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文