如何在 ruby​​ 中使用 Selenium WebDriver (selenium 2.0) 客户端设置选定的选项 [英] How do I set an option as selected using Selenium WebDriver (selenium 2.0) client in ruby

查看:32
本文介绍了如何在 ruby​​ 中使用 Selenium WebDriver (selenium 2.0) 客户端设置选定的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试熟悉新的 ruby​​ selenium-webdriver,因为它看起来比以前版本的 selenium 和随附的 ruby​​ 驱动程序更直观.另外,我无法让旧的 selenium 在 windows 中与 ruby​​ 1.9.1 一起使用,所以我想我会寻找替代方案.到目前为止,我已经用我的脚本完成了这项工作:

I am trying to get familiar with the new ruby selenium-webdriver as it appears more intuitive mostly than the previous version of selenium and the ruby driver that went with it. Also, i had trouble getting the old selenium to work with ruby 1.9.1 in windows so I thought i'd look for an alternative. So far i've done this with my script:

require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
driver.get "https://example.com"

element = driver.find_element(:name, 'username')
element.send_keys "mwolfe"
element = driver.find_element(:name, 'password')
element.send_keys "mypass"
driver.find_element(:id, "sign-in-button").click
driver.find_element(:id,"menu-link-my_profile_professional_info").click
driver.find_element(:id,"add_education_btn").click
country_select = driver.find_element(:name, "address_country")

所以基本上我正在登录我的网站并尝试将教育条目添加到我的用户配置文件中..我有一个带有选项的选择框的引用(在 country_select 变量中),现在我想选择一个选项给定的值.. 我不知道如何在新客户端中执行此操作.. 我唯一能想到的就是遍历所有选项,直到找到我想要的选项,然后调用 execute_script:http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html#execute_script-class_method设置 selectedIndex 的方法.

So basically I'm logging into my site and trying to add an education entry to my user profile.. I have a reference to a select box with options (in the country_select variable) and now i want to select an option with a given value.. I don't see how to do this in the new client.. The only thing I can think of doing is looping through all the options until I find the one I want, and then call execute_script: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html#execute_script-class_method method to set the selectedIndex.

有没有其他方法可以做到这一点?在 selenium 2.0/webdriver 的 java api 中:http://seleniumhq.org/docs/09_webdriver.html有一个这样做的例子

Is there any other way to do this? In the java api for selenium 2.0/webdriver here: http://seleniumhq.org/docs/09_webdriver.html there is an example of doing this

Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText("Edam");

除非我遗漏了什么,否则 ruby​​ 版本似乎没有这个功能.任何帮助将不胜感激.

It doesn't appear that the ruby version has this feature though unless I'm missing something. Any help would be appreciated.

推荐答案

请注意,以上方法均无效.Element#selectElement#toggle 已被弃用.您需要执行以下操作:

Please note that none of the above will work anymore. Element#select and Element#toggle have been deprecated. You need to do something like:

my_select.click
my_select.find_elements( :tag_name => "option" ).find do |option|
  option.text == value
end.click

这篇关于如何在 ruby​​ 中使用 Selenium WebDriver (selenium 2.0) 客户端设置选定的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆