Webdriver API(8)列表操作

**

操作单选下拉列表

**
被测网页HTML代码:

<html>
<meta charset="UTF-8">
<body>
	<select name='fruit'size=1>
		<option id ='peach'value='taozi'>桃子</option>
		<option id='watermelon'value='xigua'>西瓜</option>
		<option id='orange'value='juzi'select="select">橘子</option>
		<option id='kiwifruit'value='mihoutao'>猕猴桃</option>
		<option id='maybush'value='shanzha'>山楂</option>
		<option id='litchi'value='lizhi'>荔枝</option>
	</select>
</body>
</html>

遍历所有选项并打印选项显示的文本和选项值:

from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_printSelectText(self):
        url="file:///F:/5.html"
        self.driver.get(url)
        select=self.driver.find_element_by_name("fruit")
        all_options=select.find_elements_by_tag_name("option")
        for option in all_options:
            print("选项显示的文本:",option.text)
            print("选项值为:",option.get_attribute("value"))
            option.click()
            import time
            time.sleep(1)
    def tearDown(self):
        self.driver.quit()
if __name__ == '__main__':
    unittest.main()

这段代码里要注意all_options=select.find_elements_by_tag_name(“option”)中,elements是复数,忘加s会出类型错误
选择下拉列表元素的三种方法
html代码同上

#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_operateDropList(self):
        url="file:///F:/5.html"
        self.driver.get(url)
        from selenium.webdriver.support.ui import Select
        select_element=Select(self.driver.find_element_by_xpath("//select"))
        print(select_element.first_selected_option.text)
        all_options=select_element.options
        print(len(all_options))
        '''
        is_enabled():判断元素是否可操作
        is_selected():判断元素是否可被选中
        '''
        if all_options[1].is_enabled()and not all_options[1].is_selected():
            #方法一:
            select_element.select_by_index(1)
            print(select_element.all_selected_options[0].text)
            self.assertEqual(select_element.all_selected_options[0].text,u"西瓜")
        	import time
        	time.sleep(2)
            #方法二:
            select_element.select_by_visible_text("猕猴桃")
            self.assertEqual(select_element.all_selected_options[0].text,"猕猴桃")
        	import time
        	time.sleep(2)
            #方法三:
            select_element.select_by_value("shanzha")
            print(select_element.all_selected_options[0].text)
    def tearDown(self):
        self.driver.quit()
if __name__ == '__main__':
    unittest.main()

说明:
1、select_element.all_selected_options属性获取的是所有选中项的对象组成的列表对象,由于例子是单选,因此选项只有一个,通过select_element.all_selected_options[0].text来获取选中项的文本内容
2、IndentationError: unexpected indent 这个语法错误一般是缩进出错了
断言单选列表选项值
html代码同上

#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_checkSlecetText(self):
        url="file:///F:/5.html"
        self.driver.get(url)
        from selenium.webdriver.support.ui import Select  #使用Xpath定位方式获取select页面元素对象
        select_element=Select(self.driver.find_element_by_xpath("//select"))#获取所有选项的页面元素对象
        actual_options=select_element.options
        expect_optionsList=["桃子","西瓜","橘子","猕猴桃","山楂","荔枝"]
        actual_optionsList=list(map(lambda option:option.text,actual_options))
        print(actual_optionsList)
        self.assertListEqual(expect_optionsList,actual_optionsList)

    def tearDown(self):
        self.driver.quit()

    if __name__ == '__main__':
        unittest.main()

说明:
用了map函数,map函数在py3中返回的是iterators,不是list
解决办法:actual_optionsList=list(map(lambda option:option.text,actual_options))
前面添加list

操作多选的选择列表

html代码:

<html>
<meta charset="UTF-8">
<body>
	<select name='fruit'size=6 multiple=true>
		<option id ='peach'value='taozi'>桃子</option>
		<option id ='orang'value='juzi'>橘子</option>
		<option id ='kiwifruit'value='mihoutao'>猕猴桃</option>
		<option id ='mybush'value='shanzha'>山楂</option>
		<option id ='lithchi'value='lizhi'>荔枝</option>
		
	</select>
</body>
</html>

实例:

#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_oprateMultipleOptionDropList(self):
        url="file:///F:/6.html"
        self.driver.get(url)
        from selenium.webdriver.support.ui import Select
        import time
        select_element=Select(self.driver.find_element_by_xpath("//select"))
        select_element.select_by_index(0)
        select_element.select_by_visible_text("山楂")
        select_element.select_by_value("mihoutao")
        for option in select_element.all_selected_options:
            print(option.text)
        select_element.deselect_all()
        time.sleep(2)
        print("------------再次选中3个选项----------")
        select_element.select_by_index(1)
        select_element.select_by_visible_text("荔枝")
        select_element.select_by_value("juzi")
        select_element.deselect_by_visible_text("荔枝")
        select_element.deselect_by_index(1)
        select_element.deselect_by_value("juzi")
    def tearDown(self):
        self.driver.quit()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值