練習的html代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下拉框練習</title>
</head>
<body>
<select name="辛棄疾" id="">
<option value="01">破陣子·爲陳同甫賦壯詞以寄之</option>
<option value="02">醉裏挑燈看劍,夢迴吹角連營。</option>
<option value="03">八百里分麾下炙,五十弦翻塞外聲。沙場秋點兵。</option>
<option value="04">馬作的盧飛快,弓如霹靂弦驚。</option>
<option value="05">了卻君王天下事,贏得生前身後名。</option>
<option value="06">可憐白髮生!</option>
</select>
</body>
</html>
select方法主要有三類
select_by_index(self, index) #以index屬性值來查找匹配的元素並選擇;
select_by_value(self, value) #以value屬性值來查找該option並選擇;
select_by_visible_text(self, text) #以text文本值來查找匹配的元素並選擇;
first_selected_option(self) #選擇第一個option 選項 ;
使用以上三類方法做個簡單的練習
from selenium.webdriver.support.select import Select
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get("file:///C:/Users/ccl/PycharmProjects/untitled2/ccl/selenium_test/select_test.html")
opt = driver.find_element_by_name('辛棄疾')
Select(opt).select_by_visible_text('醉裏挑燈看劍,夢迴吹角連營。!')
sleep(1)
Select(opt).select_by_index(1)
sleep(1)
Select(opt).select_by_value('03')
driver.quit()
針對 select_by_index 配合while循環做個練習,效果如GIF
某些博主的封裝,[https://blog.csdn.net/u014703798/article/details/84928079]
1.BasePage封裝select操作:
def select_option(self,locator,value,type="index"):
self.wait_utilVisible(locator)
se=self.get_element(locator)
logging.info("選擇的type爲{0}".format(type))
if type=="index":
Select(se).select_by_index(value)
logging.info("選擇的index是{0}".format(value))
elif type=="value":
Select(se).select_by_value(value)
logging.info("選擇的值是{0}".format(value))
else:
Select(se).select_by_visible_text(value)
logging.info("根據文本內容傳的值是{0}".format(value))
2.select下拉框元素定位
# 查詢輸入框-在職狀態
isLeave=(By.XPATH,"//select[@ng-model='filterOptions.IsLeave']")
3.功能Page中調用
# 查詢正常狀態的值
def search_by_isLeave(self):
self.select_option(self.isLeave,1,type="index")
self.get_element(self.Button_Search).click()
# 獲取第7列的數據
def userlist_data_isleave(self):
text=self.get_table_list(self.list_7)
logging.info("獲取的列表信息是{0}".format(text))
return text
4.TestCase中使用
def test_search_by_isleave(self,login_User):
UserlistPage(login_User[0]).search_by_isLeave()
assert "離職" not in UserlistPage(login_User[0]).userlist_data_isleave()