如何在ruby中设置使用Selenium WebDriver(Selenium 2.0)客户端选择的选项

如何在ruby中设置使用Selenium WebDriver(Selenium 2.0)客户端选择的选项

问题描述:

我正在尝试熟悉新的ruby selenium-webdriver,因为它似乎比以前的硒版本及其随附的ruby驱动程序更加直观.另外,我在使旧硒与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");

不过,除非我缺少某些功能,否则红宝石版本似乎没有此功能. 任何帮助将不胜感激.

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