Skip to content

Instantly share code, notes, and snippets.

@peterfarrell
Forked from jeremi/test.py
Last active August 29, 2015 14:20
Show Gist options
  • Save peterfarrell/4d44e295c3df302ff25f to your computer and use it in GitHub Desktop.
Save peterfarrell/4d44e295c3df302ff25f to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment