Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an option as selected using Selenium WebDriver (selenium 2.0) client in ruby

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.

like image 331
Matt Wolfe Avatar asked Jan 12 '11 18:01

Matt Wolfe


People also ask

How do you right click and select Options in Selenium?

Selenium uses the Actions class to perform the right click action. The contextClick() is a method under Actions class to do the right click and once the menu opens, we can select an option from them via automation.

How do I use Selenium in Ruby?

New Selenium IDESelect the latest version and click on it. Click on the Save File button to download the corresponding rubyinstaller.exe file. Once the download is completed, accept the license agreement and proceed to the next steps till installation is completed.

How do I verify the default option selected in a dropdown in Selenium?

Select select = new Select(driver. findElement(By. xpath("//*[@id='listingDetailJqGrid_toppager_center']/table/tbody/tr/td[8]/select"))); WebElement option = select. getFirstSelectedOption(); Now it returns the actual element properties.


2 Answers

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)

like image 152
pnewhook Avatar answered Sep 23 '22 17:09

pnewhook


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 
like image 22
Tyson Avatar answered Sep 22 '22 17:09

Tyson