2

Im new to python, or coding for that matter... Currently I have a python script to select one option within the dropdown menu, but I would like my script to repeat each time and select the next option. Theres about 50 different options within the dropdown.

l1 = "Hong Kong, China (Chrome, Canary, Firefox)"
urlTextBox = "url"
dropdownOption = "location"
submitBtn = ".//*[@id='start_test-container']/p/input"
homeBtn = ".//*[@id='nav']/li[1]/a"
urlTextBoxElement = WebDriverWait(driver, 10).\
 until(lambda driver: driver.find_element_by_id(urlTextBox))
dropdownOptionElement = WebDriverWait(driver, 10).\
 until(lambda driver: driver.find_element_by_id(dropdownOption))
submitBtnElement = WebDriverWait(driver, 10).\
 until(lambda driver: driver.find_element_by_xpath(submitBtn))
urlTextBoxElement.send_keys(webTeamPage)
Select(dropdownOptionElement).select_by_visible_text(l1)
submitBtnElement.click()
time.sleep(3)
homeBtnElement = WebDriverWait(driver, 10).\
 until(lambda driver: driver.find_element_by_xpath(homeBtn))
asked Feb 12, 2015 at 19:58
1
  • IIUC you want to have script which will loop through the options, and provide results (one option text at a time) to another script which would perform some actions/checks with provided option? Commented Feb 12, 2015 at 20:45

1 Answer 1

1

I was able to answer my own question, Please see script below:

 urlTextBox = "url"
 dropdownOption = "location"
 submitBtn = ".//*[@id='start_test-container']/p/input"
 homeBtn = ".//*[@id='nav']/li[1]/a"
 webTeamPage = "personalwebpage.com"
 select = driver.find_element_by_id(dropdownOption) #get the select element
 options = select.find_elements_by_tag_name("option") #get all the options into a list
 optionsList = []
 for option in options: #iterate over the options, place attribute value in list
 optionsList.append(option.get_attribute("value"))
 for optionValue in optionsList:
 urlTextBoxElement = WebDriverWait(driver, 10).\
 until(lambda driver: driver.find_element_by_id(urlTextBox))
 dropdownOptionElement = WebDriverWait(driver, 10).\
 until(lambda driver: driver.find_element_by_id(dropdownOption))
 submitBtnElement = WebDriverWait(driver, 10).\
 until(lambda driver: driver.find_element_by_xpath(submitBtn))
 driver.find_element_by_id(urlTextBox).clear()
 urlTextBoxElement.send_keys(webTeamPage)
 submitBtnElement.click()
 time.sleep(3)
 homeBtnElement = WebDriverWait(driver, 10).\
 until(lambda driver: driver.find_element_by_xpath(homeBtn))
 homeBtnElement.click()
 print "starting loop on option %s" % optionValue
 select = Select(driver.find_element_by_id(dropdownOption))
 select.select_by_value(optionValue)
answered Feb 12, 2015 at 21:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.