selenium处理select下拉框

2020-08-19  本文已影响0人  S_jie
<select name="complaint_result_1" id="complaint_result_1" onchange="select_change()" lay-filter="complaint_result_1">
                    <option value="0">选择</option>
                    <option value="1">成功</option>
                    <option value="2">失败</option>
</select>

selenium处理select下拉框的方法主要有三种

select_by_index(index)         #以index属性值来查找
select_by_value(value)         #以value属性值来查找
select_by_visible_text(text)  #以text文本值来查找
first_selected_option()       #选择第一个option
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get(url)

s = drvier.find_element(By.CSS_SELECTOR, '#complaint_result_1')

# 通过文本内容定位
Select(s).select_by_visible_text('成功')
sleep(1)

# 通过select选项的索引来定位(从0开始计数)
Select(s).select_by_index(1)
sleep(1)

# 通过选项的value属性值来定位
Select(s).select_by_value('2')

# 选择第一个option 
Select(s).first_selected_option()

driver.quit()
# 返回所有选中的optionElement对象
Select(s).all_selected_options()
 
# 取消所有选中的option
Select(s).deselect_all()
 
# 通过option的index来取消对应的option
Select(s).deselect_by_index(1)
 
# 通过value属性,来取消对应option
Select(s).deselect_by_value('')
 
# 通过option的文本内容,取消对应的option
Select(s).deselect_by_visible_text('')
上一篇 下一篇

猜你喜欢

热点阅读