1

I am looking for a way to select an item in a drop-down menu in python with Splinter (very similar to selenium I guess) :

<li class="option1" rel="1158"> Option 1</li>
<li class="option2" rel="1158"> Option 2</li>

And even after many researches, I wasn't able to find out a way to click the item...

This is not working for instance : browser.find_by_class_name('option1') What should I use ?

1

I use this trick in my pageobject (in selenium, not splinter):

  • find dropdown by locator
  • loop through options in dropdown, and click on it if found

Code to find option with value option_text: dropdown = self.driver.find_element(by=byx, value=locx) found = None for option in dropdown.find_elements_by_tag_name('option'): if option.text == option_text: found = option.text # save text first, before it may get stale option.click() break # make sure desired option value was found in selection dropdown self.assertTrue(found, 'desired option was not found')

Note: variable 'found' is a hack: We use both HTML/Zope and Angular for templates. HTML/Zope option value is option text, but Angular's value is offset(index), not the text, so this does not work:

self.assertEqual(option_text, dropdown.get_attribute('value'))

so we need variable to detect if desired option was found or not.

HTH

  • Thanks, I am currently following this way, but I don't understand what you call a locator (as English is not my mother tongue). Thus, this is obscur to me : "find_element(by=byx, value=locx)" I assume that what I still don"t understand is the way to select the option without using "find_by_class_name", which, in my case, trigger this : "AttributeError: 'WebDriver' object has no attribute 'find_by_class_name' " – Thomas Dec 25 '14 at 16:44
  • by=byx, value=locx are just parameters for find_element(), as required by Python WebDriver/Selenium. Standard Selenium terminology. Locator type an locator value. English is not my first language either :-) – Peter M. - stands for Monica Jan 21 '16 at 16:29
1

Have you tried:

browser.find_by_class_name('option1').select(' Option 1')

I haven't used Splinter before, but I looked at their unit tests and saw something similar:

def test_can_select_a_option_via_element(self):
    "should provide a way to select a option via element"
    self.assertFalse(self.browser.find_option_by_value("rj").selected)
    self.browser.find_by_name("uf").select("rj")
    self.assertTrue(self.browser.find_option_by_value("rj").selected)
  1. It asserts the option having the value of "rj" is not already selected
  2. It finds an element named "uf" and selects "rj" from it
  3. It asserts the option having the value of "rj" is now selected

I think the second part is what you're trying to do. I hope that solves it.

  • Actually I tried both : browser.find_by_class_name('option1').select(' Option 1') browser.find_by_class_name('option1') And it gives me the error : " AttributeError: 'WebDriver' object has no attribute 'find_by_class_name' " For the second solution you give, it seems not to suit for the source code I have to deal with. After all, why should I use "find_by_name" or "find_by_value" ? I don't see what could help me except "find_by_class_name". The problem is that it does not work for me as explained above. Thanks anyway, if you have more suggestions, it would be a pleasure. – Thomas Dec 25 '14 at 16:34
1

Splinter does not support find_by_class_name. Splinter has support for find_by_option_by_text and find_option_by_value but it does not look like you have the option tag in the html.

Try using find_by_css instead for the list elements. This code snippet assumes your list items are visible:

browser.find_by_css('.option1')

If they are invisible, you will need to trigger the parent element by click or hover, or by forcing the list items to be visible. If this is still not working, a little more html will help in understanding exactly what is going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.