一、select下拉列表
Select类处理select-option
1)导入Select类
from selenium.webdriver.support.select import Select
2)确保你要操作的元素是select。实例化Select类
Select()
3)找到select元素对象
select_ele=driver.find_element(By.NAME,'cars')
4)将select对象作为参数
se=Select(select_ele)
5)选择下拉列表中的值
- 根据value属性选值
se.select_by_value('volvo')
- 根据文本内容选值
se.select_by_visible_text('Fiat')
- 根据下标来选,从0开始
se.select_by_index(1)
二、非select下拉列表
非Select元素的下拉列表操作流程
1、点击使用下拉列表可见
2、再在下拉列表当中选择元素
from selenium import webdriver标签:web,loc,Selenium,driver,element,Select,自动化,列表,select From: https://www.cnblogs.com/mango-93/p/18048106
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com/")
wait = WebDriverWait(driver,20)
# 点击右上角的设置,显示下拉列表
setting_loc = (By.ID,'s-usersetting-top')
wait.until(EC.visibility_of_element_located(setting_loc))
driver.find_element(*setting_loc).click()
# 下拉列表当中,选择【高级搜索】点击
advance_search_loc = (By.XPATH, "//div[@id='u1']//div//a[2]")
wait.until(EC.visibility_of_element_located(advance_search_loc))
driver.find_element(*advance_search_loc).click()
#在新的弹出框当中,点击显示下拉列表
adv_ft_div = (By.XPATH, '//div[@class="c-select adv-ft-select"]')
wait.until(EC.visibility_of_element_located(adv_ft_div))
driver.find_element(*adv_ft_div).click()
# 在下拉列表当中选择excel
excel_option_loc = (By.XPATH, '//*/p[text()="PowerPoint(.ppt)"]')
wait.until(EC.visibility_of_element_located(excel_option_loc))
driver.find_element(*excel_option_loc).click()
sleep(10)
driver.quit()