How to Set an Option as Selected Using Selenium Webdriver (Selenium 2.0) Client in Ruby

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

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

Clicking an option in DropDown menu with Selenium (Ruby)

What do you get if you do this?

chan_text = @driver.find_element(:css,'select#check-list option').text
puts chan_text

my bet is, not what you are looking for.

But if you do this -

chan_text = @driver.find_element(:css,'select#check-list option:nth-child(2)').text
puts chan_text #"CHEESE" is what will be returned

The reason being your selector for the find_element (for chan_text), returns all the option values, where as I assume you only want one. So appending :nth-child(n) in your css selector, will help you select the option you are particularly interested in.

undefined method select_list using ruby with watir 2.0 & selenium web driver

I can think of two possible reasons for the exception.

Browser is the wrong type

Based on the exception, it is saying that browser is a Selenium::Webdriver object. To use the select_list method you need a Watir::Browser (or Watir::Element) object.

I am guessing that somewhere you have the line:

browser = Selenium::WebDriver.for :firefox

This is creating a Selenium browser/driver instead of a Watir browser. You want to do:

browser = Watir::Browser.new :firefox

Using incorrect version of Watir

Based on the title of the question, you are trying to use Watir 2.0 and Selenium-Webdriver. These are not compatible with each other. Watir 2.0, which is now Watir-Classic, does not use Selenium-Webdriver at all.

If you want to use Selenium-Webdriver with a Watir API (ie methods like select_list), you need to use the Watir-Webdriver gem.

In other words, the top of your script would have:

require 'watir-webdriver'
browser = Watir::Browser.new :ff

Or, if you have the Watir meta gem installed, using any browser other than IE would cause the Watir-Webdriver gem to load:

require 'watir'
browser = Watir::Browser.new :ff

Capture selected value from list using webdriver and Ruby

You can use below code select list items:

cars_select = driver.find_element(:id=> "cars_list") 

//use id of your dropdownlist

options = cars_select.find_elements(:tag_name=>"option")
options.each do |el|
if (el.value == "Honda")
el.select()
var selected_car = el.value;
break
end
end

How to select/get drop down option in Selenium 2

Take a look at the section about filling in forms using webdriver in the selenium documentation and the javadoc for the Select class.

To select an option based on the label:

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

To get the first selected value:

WebElement option = select.getFirstSelectedOption()


Related Topics



Leave a reply



Submit