select 下拉表单操作
方法一:1.先定位到select,然后定位到option
方法二:调用select方法,实例化一个对象,然后调用select_by_value方法,还有index方法,text方法,可参考源代码
from selenium.webdriver import Chrome
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = Chrome()
driver.get("https://www.baidu.com")
# 定位到设置按钮,然后点击
driver.find_element_by_xpath("//a[contains(@href,'preferences.html')]").click()
# 等到高级搜索出现,点击进入
wait = WebDriverWait(driver,20)
wait.until(EC.element_to_be_clickable((By.XPATH,"//a[contains(@href,'advanced.html')]"))).click()
# 方法一,先定位select,再定位option
# 等到文档格式“所有网页和文件出现”
# myselect = wait.until(EC.element_to_be_clickable((By.NAME,"ft")))
# myselect.click()
# # 定位到option
# myoption = wait.until(EC.element_to_be_clickable((By.XPATH,"//option[@value='ppt']")))
# myoption.click()
# 方法二
# 等到文档格式“所有网页和文件出现”
myselect = wait.until(EC.element_to_be_clickable((By.NAME,"ft")))
myselect.click()
select_obj = Select(myselect)
select_obj.select_by_value("pdf")