8

I am more familiar with Java and Selenium than Ruby and Capybara and SitePrism, so apologies if this question is rather thick.

Selenium has a very useful class for managing Select tags, Selenium::WebDriver::Support::Select, which can be created by passing it the Selenium Element (Selenium::WebDriver::Element) representing the select. I would like to get a Select object so I can use its convenient methods.

Using SitePrism and Capybara, however, the standard method of defining elements gives me access to a select modeled by Capybara's Element class, Capybara::Node::Element, and I can't seem to find an easy way to extract the underlying Selenium Element from the Capybara Element.

I've searched around for alternatives and found Capybara's #select method, but that seems very limiting to me, since it looks like it forces you to select by value and has very narrow parameters for defining the select on the page.

Is there an easy way to create a Selenium Select from SitePrism/Capybara? Or is there a better way of doing this entirely? Thanks!

James Martineau
  • 951
  • 1
  • 13
  • 29
  • Can you share the code,that you are using for this ? – Arup Rakshit Sep 10 '13 at 18:25
  • @Babai Well, the Select class linked above provides the #select_by method which accepts both how and what as arguments. This means that with a Selenium Select, you can select an option using its value, text, or index. And since the Select you are using is created from any Element, you could locate said Element any way you like - using a CSS Selector (my preferred method) or XPath, for instance. Capybara's #select method only allows you to select options by value, and forces you to identify the select by id/name/label only. Sorry, I'm not sure what kind of code example would be useful here. – James Martineau Sep 10 '13 at 18:54
  • I think you can do what you want using the standard Capybara methods. For example, you could select an option via css using `page.find(:css, 'option[value="1"]').select_option`. – Justin Ko Sep 10 '13 at 20:48

1 Answers1

8

Ah, I found it. It was right in Capybara::Node::Element all along. The #native method returns the native element from the driver. This can then be passed into the Selenium Select's initialize method to successfully create the Select.

James Martineau
  • 951
  • 1
  • 13
  • 29
  • Thanks for this. I couldn't find an answer to the problem of incorrect element type anywhere, but using `my_element.native` in the function call fixed it. – februaryInk Jun 19 '15 at 02:20