如何对下拉列表中的值进行排序,并通过Selenium和Python删除下拉列表中的重复值

如何对下拉列表中的值进行排序,并通过Selenium和Python删除下拉列表中的重复值,python,list,selenium,selenium-webdriver,webdriver,Python,List,Selenium,Selenium Webdriver,Webdriver,您可以使用以下代码来获得不重复的已排序月份: dropdownoption = driver.find_elements_by_xpath("//*[@id='month']/option") print len(dropdownoption) for val in dropdownoption: print val.text 输出是 monthes = sorted(set([node.text for node in driver.find_elements_by_xpath

您可以使用以下代码来获得不重复的已排序月份:

dropdownoption = driver.find_elements_by_xpath("//*[@id='month']/option") 
print len(dropdownoption) 
for val in dropdownoption: 
    print val.text
输出是

monthes = sorted(set([node.text for node in driver.find_elements_by_xpath("//*[@id='month']/option")]))
如果要跳过第一个(
“月”
)选项,请尝试替换

['Apr', 'Aug', 'Dec', 'Feb', 'Jan', 'Jul', 'Jun', 'Mar', 'May', 'Month', 'Nov', 'Oct', 'Sept']


要对下拉列表的值进行排序,并通过Selenium和Python删除下拉列表中的重复值,可以使用以下解决方案:

driver.find_elements_by_xpath("//*[@id='month']/option")[1:]

这完全符合我的要求

months = []
sorted_months = []
unique_months = []
unique_sorted_months = []
dropdownoption = driver.find_elements_by_xpath("//*[@id='month']/option") 
print len(dropdownoption) 
for val in dropdownoption: 
    months.append(val.text)
print("Printing the actual list of months:")
print(months)

#Output>>>['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec', 'Dec', 'Jan', 'Jun']

print("Printing the available months in sorted order:")
sorted_months = sorted(months)
print(sorted_months)

#Output>>>['Apr', 'Aug', 'Dec', 'Dec', 'Feb', 'Jan', 'Jan', 'Jul', 'Jun', 'Jun', 'Mar', 'May', 'Month', 'Nov', 'Oct', 'Sept']

print("To remove the duplicate and print the list:")
unique_months= set(months)
print(unique_months)

#Output>>>{'May', 'Jul', 'Feb', 'Mar', 'Oct', 'Nov', 'Jun', 'Jan', 'Aug', 'Sept', 'Month', 'Apr', 'Dec'}

print("To remove the duplicates and print the available months in sorted order:")
unique_sorted_months = sorted(set(months))
print(unique_sorted_months)

#Output>>>['Apr', 'Aug', 'Dec', 'Feb', 'Jan', 'Jul', 'Jun', 'Mar', 'May', 'Month', 'Nov', 'Oct', 'Sept']

下拉列表的属性是:。。。完成以下代码后,我能够读取下拉列表中的所有值,但无法对重复值进行排序和删除:dropdownpoption=driver.find_elements_by_xpath(“/*[@id='month']]/option”)print len(dropdownpoption)对于下拉列表中的val选项:print val.text您能否尊重为您撰写答案的人?您的解决方案不正确,因为
唯一\u排序\u月份
绝对没有排序。您可以将代码修复为排序(设置(…),但是。。。。这与我的解决方案完全相同。因此,您可以删除您的答案,而不创建重复项。。。那是我的错。更正了,你检查过了吗?请注意,1)您正在隐藏
list
内置函数(不要将变量调用为
list
)2)
sort()
对现有列表执行排序操作,而
sorted()
动态返回排序列表(为您节省1个额外的代码行)@muddiser您是否有机会签出我更新的列表。如果你觉得好看,就告诉我。
driver.find_elements_by_xpath("//*[@id='month']/option")[1:]
months = []
sorted_months = []
unique_months = []
unique_sorted_months = []
dropdownoption = driver.find_elements_by_xpath("//*[@id='month']/option") 
print len(dropdownoption) 
for val in dropdownoption: 
    months.append(val.text)
print("Printing the actual list of months:")
print(months)

#Output>>>['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec', 'Dec', 'Jan', 'Jun']

print("Printing the available months in sorted order:")
sorted_months = sorted(months)
print(sorted_months)

#Output>>>['Apr', 'Aug', 'Dec', 'Dec', 'Feb', 'Jan', 'Jan', 'Jul', 'Jun', 'Jun', 'Mar', 'May', 'Month', 'Nov', 'Oct', 'Sept']

print("To remove the duplicate and print the list:")
unique_months= set(months)
print(unique_months)

#Output>>>{'May', 'Jul', 'Feb', 'Mar', 'Oct', 'Nov', 'Jun', 'Jan', 'Aug', 'Sept', 'Month', 'Apr', 'Dec'}

print("To remove the duplicates and print the available months in sorted order:")
unique_sorted_months = sorted(set(months))
print(unique_sorted_months)

#Output>>>['Apr', 'Aug', 'Dec', 'Feb', 'Jan', 'Jul', 'Jun', 'Mar', 'May', 'Month', 'Nov', 'Oct', 'Sept']
dropdownoption = driver.find_elements_by_xpath("//*[@id='month']/option")    
print len(dropdownoption)
for val in dropdownoption:
    container=val.text
    list.append(container)
list.sort()
print ' '.join(list)
st=set(list)
print ' '.join(st)