Python?selenium下拉選擇框?qū)崙?zhàn)應(yīng)用例子
一、前言
selenium的下拉選擇框。我們通常會遇到兩種下拉框,一種使用的是html的標(biāo)簽select,另一種是使用input標(biāo)簽做的假下拉框。
后者我們通常的處理方式與其他的元素類似,點(diǎn)擊或使用JS等。而對于前者,selenium給了有力的支持,就是Select類。
進(jìn)行測試的網(wǎng)站:http://sahitest.com/demo/selectTest.htm
網(wǎng)頁及對應(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)建一個Chrome WebDriver的實(shí)例
self.driver = webdriver.Chrome()
# 選擇頁面第一個下拉框,依次選擇值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
# 選擇第二個選項(xiàng)o1
s1.select_by_index(1)
time.sleep(3)
# 為了方便查看效果,可以加上等待時間
time.sleep(3)
# 選擇value="o2"的項(xiàng),value是option標(biāo)簽的一個屬性值,并不是顯示在下拉框中的值
s1.select_by_value("o2")
# 查看選中選擇框的默認(rèn)值
print s1.first_selected_option.text
time.sleep(3)
# 選擇text="o3"的值,即在下拉時我們可以看到的文本,visible_text是在option標(biāo)簽中間的值,是顯示在下拉框的值
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)
# 根據(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的下拉框,否則會報(bào)錯
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)的順序,第一個為 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提供了三個屬性方法給我們必要的信息:
options ——提供所有的選項(xiàng)的列表,其中都是選項(xiàng)的WebElement元素
all_selected_options ——提供所有被選中的選項(xiàng)的列表,其中也均為選項(xiàng)的WebElement元素
first_selected_option ——提供第一個被選中的選項(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()到此這篇關(guān)于Python selenium下拉選擇框的文章就介紹到這了,更多相關(guān)Python selenium下拉選擇框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實(shí)例,本文直接給出操作mysql代碼實(shí)例,包含創(chuàng)建表、插入數(shù)據(jù)、插入多條數(shù)據(jù)、查詢數(shù)據(jù)等內(nèi)容,需要的朋友可以參考下2015-04-04
pandas將DataFrame的幾列數(shù)據(jù)合并成為一列
本文主要介紹了pandas將DataFrame的幾列數(shù)據(jù)合并成為一列,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
詳解python實(shí)現(xiàn)讀取郵件數(shù)據(jù)并下載附件的實(shí)例
這篇文章主要介紹了詳解python讀取郵件數(shù)據(jù)并下載附件的實(shí)例的相關(guān)資料,這里提供實(shí)現(xiàn)實(shí)例,幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
Python+Turtle實(shí)現(xiàn)繪制可愛的小倉鼠
肉嘟嘟的小動物很是可愛,這篇文章主要為大家介紹一下如何運(yùn)用Python中的turtle庫控制函數(shù)繪制小倉鼠,文中的實(shí)現(xiàn)方法講解詳細(xì),感興趣的可以嘗試一下2022-10-10
對python requests發(fā)送json格式數(shù)據(jù)的實(shí)例詳解
今天小編就為大家分享一篇對python requests發(fā)送json格式數(shù)據(jù)的實(shí)例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python實(shí)現(xiàn)Event回調(diào)機(jī)制的方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)Event回調(diào)機(jī)制的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
使用Pandas計(jì)算系統(tǒng)客戶名稱的相似度
在日常業(yè)務(wù)處理中,我們經(jīng)常會面臨將不同系統(tǒng)中的數(shù)據(jù)進(jìn)行匹配和比對的情況,本文將介紹如何使用Python的Pandas庫來處理這個問題,需要的可以參考一下2023-07-07

