Skip to content

Instantly share code, notes, and snippets.

@jeremi
Created February 8, 2012 11:50
Show Gist options
  • Save jeremi/1768479 to your computer and use it in GitHub Desktop.
Save jeremi/1768479 to your computer and use it in GitHub Desktop.
Chosen test using selenium web driver in Python
from selenium import webdriver
def select_from_chosen(driver, id, value):
chosen = driver.find_element_by_id(id + '_chzn')
results = chosen.find_elements_by_css_selector(".chzn-results li")
found = False
for result in results:
if result.text == value:
found = True
break
if found:
chosen.find_element_by_css_selector("a").click()
result.click()
return found
def select_from_multi_chosen(driver, id, values):
chosen = driver.find_element_by_id(id + '_chzn')
results = chosen.find_elements_by_css_selector(".chzn-results li")
for value in values:
found = False
for result in results:
if result.text == value:
found = True
break
if found:
chosen.find_element_by_css_selector("input").click()
result.click()
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
# go to the google home page
driver.get("http://harvesthq.github.com/chosen/")
#on the demo page, ids are always changing
chosen_selects = driver.find_elements_by_css_selector(".chzn-select")
select = chosen_selects[0]
select_id = select.get_attribute("id")
select_from_chosen(driver, select_id, "France")
select = chosen_selects[1]
select_id = select.get_attribute("id")
select_from_multi_chosen(driver, select_id, ["India", "France"])
driver.quit()
@jdragojevic
Copy link

this is awesome, thank you.

@chris-ramon
Copy link

Thanks for sharing, really helpfull

@alensiljak
Copy link

Useful. Thanks!
Just an observation, though: Chozens often go crazy and select the previous element instead of the desired one.

@LyleScott
Copy link

I did this. Seems more like what the user would do.

def chozen_select(self, element_id, value):
    """
    Select a single value from a chozen drop down.

    :param element_id: The element id to grab.
    :type element_id: str
    :param value: The value of the option that is displayed.
    :type value: str
    """
    self.chozen_muliselect(element_id, [value])

def chozen_muliselect(self, element_id, values):
    """Helper for chozen multiselects.

    :param element_id: The id of the chozen element.
    :type element_id: str
    :param values: An iterable of values to set or the atom to set.
    :type values: list, tuple
    """
    if not element_id.endswith('_chzn'):
        element_id += '_chzn'

    chozen = cls.browser.find_element_by_id(element_id)
    chozen.click()
    search = chozen.find_element_by_css_selector('div.chzn-search > input')
    for value in values:
        search.send_keys(value)
        search.send_keys(Keys.ENTER)

@YogeshBendake
Copy link

please send me script in java to sselect element from chosen dropdown

@llund
Copy link

llund commented Aug 15, 2018

Note that in more recent versions of ChosenJS, the class and id names have chosen instead of chzn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment