python自動(dòng)化測(cè)試selenium操作下拉列表實(shí)現(xiàn)
處理下拉列表需要使用selenium中的工具類Select,常用方法如下:

示例網(wǎng)站:http://sahitest.com/demo
示例場(chǎng)景:打開Sahi Tests頁(yè)面,
(1)點(diǎn)擊“Select Test”頁(yè)面,鼠標(biāo)點(diǎn)擊頁(yè)面中第一個(gè)下拉列表。


示例腳本:
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.select import Select
class TestSelected(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get("https://sahitest.com/demo/")
def test_selected(self):
#點(diǎn)“Select Test”鏈接
self.driver.find_element_by_xpath("/html/body/table/tbody/tr/td[1]/a[4]").click()
#點(diǎn)第一個(gè)下拉框
se=self.driver.find_element_by_id("s1Id")
#選中下拉框選項(xiàng)
select=Select(se)
#循環(huán)打印下拉框選項(xiàng)
for options in select.options:
print(options.text)
運(yùn)行結(jié)果:

(2)操作多選列表

示例腳本:
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.select import Select
class TestSelected(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get("https://sahitest.com/demo/")
def test_multiselected(self):
#點(diǎn)“Select Test”鏈接
self.driver.find_element_by_xpath("/html/body/table/tbody/tr/td[1]/a[4]").click()
#列表多選框
mulsel = self.driver.find_element_by_id("s4Id")
select2 = Select(mulsel)
#選擇列表中所有選項(xiàng)
for i in range(6):
select2.select_by_index(i)
#根據(jù)索引值反選
# select2.deselect_by_index(i)
sleep(1)
sleep(2)
#反選所有
select2.deselect_all()
self.driver.quit()
以上:極客時(shí)間課程:selenium自動(dòng)化測(cè)試學(xué)習(xí)總結(jié)!
以上就是python自動(dòng)化測(cè)試selenium操作下拉列表實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于selenium操作下拉列表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用Python爬蟲破解滑動(dòng)驗(yàn)證碼的案例解析
今天分享個(gè)如何簡(jiǎn)單處理滑動(dòng)圖片的驗(yàn)證碼的案例,主要是使用Python爬蟲破解滑動(dòng)驗(yàn)證碼的相關(guān)實(shí)現(xiàn)代碼,感興趣的朋友跟隨小編一起看看吧2021-05-05
python實(shí)現(xiàn)應(yīng)用程序在右鍵菜單中添加打開方式功能
這篇文章主要介紹了python實(shí)現(xiàn)應(yīng)用程序在右鍵菜單中添加打開方式功能,本文分步驟給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01
2021年值得向Python開發(fā)者推薦的VS Code擴(kuò)展插件
這篇文章主要介紹了2021年值得向Python開發(fā)者推薦的VS Code擴(kuò)展插件,幫助大家更好的利用vscode進(jìn)行python的開發(fā),感興趣的朋友可以了解下2021-01-01
Python數(shù)據(jù)分析之Excel和Text文件的讀寫操作方法
Python操作Excel分為兩個(gè)主要形式,讀寫和交互式操作,可以用不同的第三方工具,下面這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)分析之Excel和Text文件的讀寫操作方法,需要的朋友可以參考下2024-08-08

