Python?selenium下拉選擇框?qū)崙?zhàn)應(yīng)用例子
一、前言
selenium的下拉選擇框。我們通常會遇到兩種下拉框,一種使用的是html的標(biāo)簽select,另一種是使用input標(biāo)簽做的假下拉框。
后者我們通常的處理方式與其他的元素類似,點擊或使用JS等。而對于前者,selenium給了有力的支持,就是Select類。
進行測試的網(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
三、選擇、反選、選項的實戰(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的實例 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標(biāo)簽的一個屬性值,并不是顯示在下拉框中的值 s1.select_by_value("o2") # 查看選中選擇框的默認值 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) # 取消選中所有選項 s4.deselect_all() # 查看選中項目 """ 輸出結(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')) # 查看選擇框的默認值 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
四、總結(jié)
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():選項的文本內(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使用urlparse分析網(wǎng)址中域名的方法
這篇文章主要介紹了python使用urlparse分析網(wǎng)址中域名的方法,涉及Python使用urlparse模塊操作URL的技巧,需要的朋友可以參考下2015-04-04Python計算一個給定時間點前一個月和后一個月第一天的方法
這篇文章主要介紹了Python計算一個給定時間點前一個月和后一個月第一天的方法,涉及Python使用datetime模塊計算日期時間的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05Python3+pycuda實現(xiàn)執(zhí)行簡單GPU計算任務(wù)
GPU的加速技術(shù)在深度學(xué)習(xí)、量子計算領(lǐng)域都已經(jīng)被廣泛的應(yīng)用。這篇文章就來和大家聊聊Python3如何利用pycuda執(zhí)行簡單GPU計算任務(wù)?,感興趣的可以了解一下2023-03-03Python中如何將Tqdm與Asyncio結(jié)合使用呢
這篇文章主要和大家詳細介紹了在Python中如何將Tqdm與Asyncio結(jié)合使用呢,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05