这篇教程python自动化测试selenium操作下拉列表实现写得很实用,希望能帮到您。 处理下拉列表需要使用selenium中的工具类Select,常用方法如下: 
示例网站:http://sahitest.com/demo 示例场景:打开Sahi Tests页面, (1)点击“Select Test”页面,鼠标点击页面中第一个下拉列表。 

示例脚本: from selenium import webdriverfrom time import sleepfrom selenium.webdriver.support.select import Selectclass TestSelected(object): def setup(self): self.driver = webdriver.Chrome() self.driver.get("https://sahitest.com/demo/") def test_selected(self): #点“Select Test”链接 self.driver.find_element_by_xpath("/html/body/table/tbody/tr/td[1]/a[4]").click() #点第一个下拉框 se=self.driver.find_element_by_id("s1Id") #选中下拉框选项 select=Select(se) #循环打印下拉框选项 for options in select.options: print(options.text) 运行结果: 
(2)操作多选列表 
示例脚本: from selenium import webdriverfrom time import sleepfrom selenium.webdriver.support.select import Selectclass TestSelected(object): def setup(self): self.driver = webdriver.Chrome() self.driver.get("https://sahitest.com/demo/") def test_multiselected(self): #点“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) #选择列表中所有选项 for i in range(6): select2.select_by_index(i) #根据索引值反选 # select2.deselect_by_index(i) sleep(1) sleep(2) #反选所有 select2.deselect_all() self.driver.quit() 以上:极客时间课程:selenium自动化测试学习总结! 以上就是python自动化测试selenium操作下拉列表实现的详细内容,更多关于selenium操作下拉列表的资料请关注51zixue.net其它相关文章! python自动化测试selenium操作checkbox和radiobox技术 python自动化测试selenium核心技术三种等待方式详解 |