练习的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()