如何使用Selenium Webdriver和Python获取ul li下拉列表中的所有选项?

时间:2017-06-23 00:35:24

标签: python selenium selenium-webdriver

我正在处理的项目将下拉列表从简单更改为ul li。在此之前从下拉列表中删除所有选项,我将执行以下操作:

myList = [] 
el = browser.find_element_by_id('Filter_ClientDistrict')
for option in el.find_elements_by_tag_name('option'):
    myList.append(option.get_attribute("value"))

如果现在的代码如下所示,如何从同一下拉列表中删除所有选项:

enter image description here

1 个答案:

答案 0 :(得分:2)

应该是

browser.find_element_by_css_selector('#Filter_ClientDistrict button.dropdown-toggle').click()
WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#Filter_ClientDistrict ul li"))
    myList = [] 
    el = browser.find_element_by_id('Filter_ClientDistrict')
    for option in el.find_elements_by_css_selector('ul li'):
        myList.append(option.get_attribute("innerText").strip())