Selenium入门21 Select操作

 select元素有单独的类:from selenium.webdriver.support.ui import Select

界面上选出select元素后,Select(select)进行类型转换就可以使用Select里的方法和属性。

#coding:utf-8
#select类

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

dr = webdriver.Firefox()
dr.get("https://kyfw.12306.cn/otn/leftTicket/init")
select = dr.find_element_by_css_selector("select#cc_start_time")
select = Select(select) #类型转换

for option in select.options: #选出所有options遍历
    option.click()
time.sleep(1)
select.select_by_index(1) #按索引选
time.sleep(1)
select.select_by_value('12001800') #按value选
time.sleep(1)
select.select_by_visible_text('18:00--24:00') #按文本选
time.sleep(1)
dr.quti()

如果是支持多选的select,还可以使用deselect系列方法。

the end!

猜你喜欢

转载自www.cnblogs.com/dinghanhua/p/9786902.html