Question

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")

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.

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.

Was it helpful?

Solution

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

OTHER TIPS

Full disclosure here: I have absolutely no working knowledge of Ruby.

However, I'm pretty good with Selenium so I think I can help. I think what you're looking for is the select method. If ruby is anything like the other drivers you can use the select method to tell one of the options it is selected.

In pseudocode/java terms it would look something like this

    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;
        }
    }

The Select object you have above is actually in a special Support package. It only exists for Java and .Net at the moment (Jan 2011)

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")

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

You can use XPath to avoid looping:

String nameYouWant = "Name you want";
WebElement select = driver.findElement(By.id(id));
WebElement option = 
  select.findElement(By.xpath("//option[contains(text(),'" + nameYouWant + "')]"));
option.click();

or

WebElement option = 
  select.findElement(By.xpath("//option[text()='" + nameYouWant + "']"));
require "selenium-webdriver"
webdriver = Selenium::WebDriver.for :firefox

driver.navigate.to url

dropdown = webdriver.find_element(:id,dropdownid)
return nil if dropdown.nil?
selected = dropdown.find_elements(:tag_name,"option").detect { |option| option.attribute('text').eql? value}
 if selected.nil? then
  puts "Can't find value in dropdown list"
 else
  selected.click
 end

In my case this is only one working sample.

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;
    }
}

Ruby Code with Example:

require "selenium-webdriver"

driver = Selenium::WebDriver.for :ie

driver.navigate.to "http://google.com"
a=driver.find_element(:link,'Advanced search')

a.click


a=driver.find_element(:name,'num')

options=a.find_elements(:tag_name=>"option")
options.each do |g|
  if g.text == "20 results"
  g.click
  break
  end
end

The easiest way I found was:

select_elem.find_element(:css, "option[value='some_value']").click

#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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top