網站首頁 編程語言 正文
一、前言
selenium的下拉選擇框。我們通常會遇到兩種下拉框,一種使用的是html的標簽select,另一種是使用input標簽做的假下拉框。
后者我們通常的處理方式與其他的元素類似,點擊或使用JS等。而對于前者,selenium給了有力的支持,就是Select類。
進行測試的網站:http://sahitest.com/demo/selectTest.htm
網頁及對應源碼:
二、關于導入方式
兩種導入方式:
from selenium.webdriver.support.ui import Select
# 或者直接從select導入
from selenium.webdriver.support.select import Select
三、選擇、反選、選項的實戰應用例子
話不多說,直接上代碼:
# -*- 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):
# 創建一個Chrome WebDriver的實例
self.driver = webdriver.Chrome()
# 選擇頁面第一個下拉框,依次選擇值O1-O3
def test_selectO1ToO3(self):
driver = self.driver
driver.get('http://sahitest.com/demo/selectTest.htm')
# 實例化Select
s1 = Select(driver.find_element_by_id('s1Id'))
# 查看選擇框的默認值
print s1.first_selected_option.text
# 選擇第二個選項o1
s1.select_by_index(1)
time.sleep(3)
# 為了方便查看效果,可以加上等待時間
time.sleep(3)
# 選擇value="o2"的項,value是option標簽的一個屬性值,并不是顯示在下拉框中的值
s1.select_by_value("o2")
# 查看選中選擇框的默認值
print s1.first_selected_option.text
time.sleep(3)
# 選擇text="o3"的值,即在下拉時我們可以看到的文本,visible_text是在option標簽中間的值,是顯示在下拉框的值
s1.select_by_visible_text("o3")
time.sleep(3)
# 反選操作,包括取消某個值和全部取消
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)
# 根據index取消選中
s4.deselect_by_index(0)
time.sleep(3)
# 根據value取消選中
s4.deselect_by_value("o1val")
time.sleep(5)
# 根據標簽文本選中
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)
# 取消選中所有選項
s4.deselect_all()
# 查看選中項目
"""
輸出結果為:
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'))
# 查看選擇框的默認值
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的下拉框,否則會報錯
? ? raise NotImplementedError("You may only deselect options of a multi-select")
NotImplementedError: You may only deselect options of a multi-select
四、總結
1、Select提供了三種選擇方法:
select_by_index(index) ——通過選項的順序,第一個為 0
select_by_value(value) ——通過value屬性
select_by_visible_text(text) ——通過選項可見文本
?2、Select提供了四種方法取消選擇:
deselect_by_index(index)
deselect_by_value(value)
deselect_by_visible_text(text)
deselect_all()
3、Select提供了三個屬性方法給我們必要的信息:
options ——提供所有的選項的列表,其中都是選項的WebElement元素
all_selected_options ——提供所有被選中的選項的列表,其中也均為選項的WebElement元素
first_selected_option ——提供第一個被選中的選項,也是下拉框的默認值
補充:三種定位方法如下
1.select_by_visible_text():選項的文本內容
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
相關推薦
- 2022-04-11 利用Python操作excel表格的完美指南_python
- 2022-10-17 Python?pywin32實現word與Excel的處理_python
- 2022-03-19 C#?壓榨cpu的辦法(推薦)_C#教程
- 2022-11-10 Android開發實現日期時間控件選擇_Android
- 2022-09-29 React實現下拉框的key,value的值同時傳送_React
- 2023-01-07 Python個人博客程序開發實例框架設計_python
- 2022-12-29 Kotlin面向對象知識點講解_Android
- 2022-10-17 Python?Django源碼運行過程解析_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支