網(wǎng)站首頁 編程語言 正文
一、前言
selenium的下拉選擇框。我們通常會(huì)遇到兩種下拉框,一種使用的是html的標(biāo)簽select,另一種是使用input標(biāo)簽做的假下拉框。
后者我們通常的處理方式與其他的元素類似,點(diǎn)擊或使用JS等。而對(duì)于前者,selenium給了有力的支持,就是Select類。
進(jìn)行測(cè)試的網(wǎng)站:http://sahitest.com/demo/selectTest.htm
網(wǎng)頁及對(duì)應(yīng)源碼:
二、關(guān)于導(dǎo)入方式
兩種導(dǎo)入方式:
from selenium.webdriver.support.ui import Select
# 或者直接從select導(dǎo)入
from selenium.webdriver.support.select import Select
三、選擇、反選、選項(xiàng)的實(shí)戰(zhàn)應(yīng)用例子
話不多說,直接上代碼:
# -*- coding: utf-8 -*-
"""
@author: lucas
@Function:
@file: selectStudy.py
@time: 2021/8/20 1:27 下午
"""
import unittest
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
class SelectStudy(unittest.TestCase):
def setUp(self):
# 創(chuàng)建一個(gè)Chrome WebDriver的實(shí)例
self.driver = webdriver.Chrome()
# 選擇頁面第一個(gè)下拉框,依次選擇值O1-O3
def test_selectO1ToO3(self):
driver = self.driver
driver.get('http://sahitest.com/demo/selectTest.htm')
# 實(shí)例化Select
s1 = Select(driver.find_element_by_id('s1Id'))
# 查看選擇框的默認(rèn)值
print s1.first_selected_option.text
# 選擇第二個(gè)選項(xiàng)o1
s1.select_by_index(1)
time.sleep(3)
# 為了方便查看效果,可以加上等待時(shí)間
time.sleep(3)
# 選擇value="o2"的項(xiàng),value是option標(biāo)簽的一個(gè)屬性值,并不是顯示在下拉框中的值
s1.select_by_value("o2")
# 查看選中選擇框的默認(rèn)值
print s1.first_selected_option.text
time.sleep(3)
# 選擇text="o3"的值,即在下拉時(shí)我們可以看到的文本,visible_text是在option標(biāo)簽中間的值,是顯示在下拉框的值
s1.select_by_visible_text("o3")
time.sleep(3)
# 反選操作,包括取消某個(gè)值和全部取消
def test_cancel_select(self):
driver = self.driver
driver.get('http://sahitest.com/demo/selectTest.htm')
s4 = Select(driver.find_element_by_id('s4Id'))
# 全選
for option in s4.options:
if not option.is_selected():
print option.text
s4.select_by_visible_text(option.text)
time.sleep(3)
# 根據(jù)index取消選中
s4.deselect_by_index(0)
time.sleep(3)
# 根據(jù)value取消選中
s4.deselect_by_value("o1val")
time.sleep(5)
# 根據(jù)標(biāo)簽文本選中
s4.deselect_by_visible_text("o2")
time.sleep(5)
# 全選
for option in s4.options:
if not option.is_selected():
s4.select_by_visible_text(option.text)
time.sleep(3)
# 取消選中所有選項(xiàng)
s4.deselect_all()
# 查看選中項(xiàng)目
"""
輸出結(jié)果為:
o1
o2
With spaces
With nbsp
"""
def test_view_selection(self):
driver = self.driver
driver.get('http://sahitest.com/demo/selectTest.htm')
s4 = Select(driver.find_element_by_id('s4Id'))
# 查看選擇框的默認(rèn)值
s4.select_by_index(1)
s4.select_by_value("o2val")
s4.select_by_visible_text("With spaces")
s4.select_by_value("o4val")
for select in s4.all_selected_options:
print select.text
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
注意:
反選(deselect)取消操作只適用于添加了multiple的下拉框,否則會(huì)報(bào)錯(cuò)
? ? raise NotImplementedError("You may only deselect options of a multi-select")
NotImplementedError: You may only deselect options of a multi-select
四、總結(jié)
1、Select提供了三種選擇方法:
select_by_index(index) ——通過選項(xiàng)的順序,第一個(gè)為 0
select_by_value(value) ——通過value屬性
select_by_visible_text(text) ——通過選項(xiàng)可見文本
?2、Select提供了四種方法取消選擇:
deselect_by_index(index)
deselect_by_value(value)
deselect_by_visible_text(text)
deselect_all()
3、Select提供了三個(gè)屬性方法給我們必要的信息:
options ——提供所有的選項(xiàng)的列表,其中都是選項(xiàng)的WebElement元素
all_selected_options ——提供所有被選中的選項(xiàng)的列表,其中也均為選項(xiàng)的WebElement元素
first_selected_option ——提供第一個(gè)被選中的選項(xiàng),也是下拉框的默認(rèn)值
補(bǔ)充:三種定位方法如下
1.select_by_visible_text():選項(xiàng)的文本內(nèi)容
from selenium.webdriver.support.select import Select
from time import sleep
from selenium import webdriver
dr=webdriver.Chrome()
dr.get('url')
dr.maximize_window()
#先定位到下拉框,通過text文本定位
Select(find_element_by_id('q')).select_by_visible_text('蒼井空')
sleep(2)
dr.quit()
2.select_by_value():value屬性定位
from selenium.webdriver.support.select import Select
from time import sleep
from selenium import webdriver
dr=webdriver.Chrome()
dr.get('url')
dr.maximize_window()
#先定位到下拉框,通過value屬性定位
Select(find_element_by_id('q')).select_by_value('3')
sleep(2)
dr.quit()
3.select_by_index():索引定位(0開始)
from selenium.webdriver.support.select import Select
from time import sleep
from selenium import webdriver
dr=webdriver.Chrome()
dr.get('url')
dr.maximize_window()
#先定位到下拉框,通過索引定位
Select(find_element_by_id('q')).select_by_index('1')
sleep(2)
dr.quit()
原文鏈接:https://blog.csdn.net/LYX_WIN/article/details/119827083
相關(guān)推薦
- 2023-02-04 Python?encode()方法和decode()方法詳解_python
- 2022-04-26 Swift踩坑實(shí)戰(zhàn)之一個(gè)字符引發(fā)的Crash_Swift
- 2022-06-01 C語言?超詳細(xì)介紹與實(shí)現(xiàn)線性表中的帶頭雙向循環(huán)鏈表_C 語言
- 2022-10-01 Docker部署單頁應(yīng)用的詳細(xì)操作_docker
- 2022-04-04 webpack-loader: loader的使用(圖片、txt文件、url、less)
- 2022-11-13 Python?argparse模塊實(shí)現(xiàn)解析命令行參數(shù)方法詳解_python
- 2023-04-20 react:理解“為了在回調(diào)中使用 `this`,這個(gè)綁定是必不可少的”
- 2022-05-12 Kotlin List的創(chuàng)建與取值 getOrElse getOrNull
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支