Without the Select class
Everything I listed in my other answer can, of course, be done with judicious use of selectors. Since the Select class doesn't exist in Ruby (to the best of my knowledge), this is the only way to work with Select options in Ruby.
Again, the HTML:
<select id="mySelectID">
<option value="Value">Option</option>
<option value="NotValue">Not Option</option>
</select>
Select by option name
Java, method 1:
In this example, we find the option via a complex xpath, then click on it:
WebElement myoption = driver.findElement(By.xpath(
"//Select[@id='mySelectID']/option[normalize-space(text())='Option']")
);
myOption.click();
Java, method 2:
In this example, we find all the options, iterate over them, and click the one we want. This is useful if you have a more complex criteria.
WebElement mySelectElm = driver.findElement(By.id("mySelectID"));
Select mySelect= new Select(mySelect);
List<WebElement> options = mySelect.getOptions();
for (WebElement option : options) {
if (option.getText().equalsIgnoreCase("Option") {
option.click();
}
}
Ruby, method 2:
Same method as the previous answer, different language:
mySelect=webdriver.find_element(:id,"mySelectID")
options=mySelect.find_elements(:tag_name=>"option")
options.each do |g|
if g.text == "Option"
g.click
break
end
end
Ruby, method 3:
Here we get fancy, using a closure to find the right option instead of a loop:
mySelect = webdriver.find_element(:id,"mySelectID")
option = dropdown.find_elements(:tag_name,"option").detect { |option| option.attribute('text').eql? "Option"}
option.click
Select by option value
Same basic idea, but we can use CSS to select an option by value instead of mucking about with xpath:
Java
WebElement myoption = driver.findElement(By.cssSelector("#mySelectID option[value='Value']"));
myOption.click();
Ruby
mySelect = webdriver.find_element(:id,"mySelectID")
option = mySelect.find_element(:css, "option[value='Value']")
option.click
Select by index
Again, easy to do with css selectors:
Java
WebElement myoption = driver.findElement(By.cssSelector("#mySelectID option:nth-child(1)"));
myOption.click();
Ruby
mySelect = webdriver.find_element(:id,"mySelectID")
option = mySelect.find_element(:css, "option:nth-child(1)")
option.click
Get the selected option
Hooray for CSS selectors! For legacy reasons, the selector for "selected" is "checked", like a checkbox:
Java:
WebElement myoption = driver.findElement(By.cssSelector("#mySelectID option:checked"));
System.out.println(myoption.getText()); //prints "Option"
Ruby
mySelect = webdriver.find_element(:id,"mySelectID")
option = mySelect.find_element(:css, "option:nth-child(1)")
print option.text
Get the list of options
Java:
List<WebElement> options = driver.findElements(By.cssSelector("#mySelectID option"));
for (WebElement option : options) {
System.out.println(option.getText()); //Prints "Option", followed by "Not Option"
}
Ruby:
mySelect=webdriver.find_element(:id,"mySelectID")
options=mySelect.find_elements(:tag_name=>"option")
options.each do |g|
print g.text #Prints "Option", followed by "Not Option"
end