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
I don't know what version Selenium this came about, but it looks like there is the Select class that pnewhook mentioned in Selenium 2.20
http://selenium.googlecode.com/svn-history/r15117/trunk/docs/api/rb/Selenium/WebDriver/Support/Select.html
option = Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath => "//select"))
option.select_by(:text, "Edam")
For the latest version of Webdriver (RC3) you should use "click()" instead of setSelected(). Also option.getText().equals("Name you want") should be used instead of option.getText()=="Name you want" in JAVA:
<!-- language: lang-java -->
WebElement select = driver.findElement(By.name("select"));
List<WebElement> options = select.findElements(By.tagName("option"));
for(WebElement option : options){
if(option.getText().equals("Name you want"){
option.click();
break;
}
}
pnewhook got it but I'd like to post the ruby version here so everyone can see it:
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.manage.timeouts.implicit_wait = 10
driver.get "https://example.com"
country_select = driver.find_element(:id=> "address_country")
options = country_select.find_elements(:tag_name=>"option")
options.each do |el|
if (el.attributes("value") == "USA")
el.click()
break
end
end
#SELECT FROM DROPDOWN IN RUBY USING SELENIUM WEBDRIVER
#AUTHOR:AYAN DATE:14 June 2012
require "rubygems"
require "selenium-webdriver"
begin
@driver = Selenium::WebDriver.for :firefox
@base_url = "http://www.yoururl.com"
@driver.manage.timeouts.implicit_wait = 30
@driver.get "http://www.yoursite.com"
#select Urugway as Country
Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "country")).select_by(:text, "Uruguay")
rescue Exception => e
puts e
@driver.quit
end