如何在selenium python脚本中创建一个循环来选择下拉菜单中的每个选项
我是python的新手,或者是这方面的编码。。。目前,我有一个python脚本可以在下拉菜单中选择一个选项,但我希望每次都重复我的脚本并选择下一个选项。下拉列表中有大约50个不同的选项 类测试(unittest.TestCase):如何在selenium python脚本中创建一个循环来选择下拉菜单中的每个选项,python,selenium,for-loop,automation,webdriver,Python,Selenium,For Loop,Automation,Webdriver,我是python的新手,或者是这方面的编码。。。目前,我有一个python脚本可以在下拉菜单中选择一个选项,但我希望每次都重复我的脚本并选择下一个选项。下拉列表中有大约50个不同的选项 类测试(unittest.TestCase): 我能够回答我自己的问题,请参见下面的脚本: urlTextBox = "url" dropdownOption = "location" submitBtn = ".//*[@id='start_test-contai
我能够回答我自己的问题,请参见下面的脚本:
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)
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)