Selenium Webdriver API(3)

Selenium Webdriver API(3)

23、操作单选下拉框 option
测试网页:test_select.html

operate_select.py
#encoding=utf-8
import unittest
import time
from selenium import webdriver

class VisitLocalWebByIE(unittest.TestCase):
    def setUp(self):
        #启动浏览器
        self.driver = webdriver.Ie(executable_path="D:\\IEDriverServer")
    def test_printSelectText(self):
        #自定义的测试网页
        url = "http://127.0.0.1:8080/test_select.html"
        #访问网页
        self.driver.get(url)
        #使用name属性找到页面上name属性为friut的下拉列表元素
        select = self.driver.find_element_by_name("fruit")
        #将所有的下拉选项存入列表
        all_options = select.find_elements_by_tag_name("option")
        #遍历下拉项列表
        for option in all_options:
            print u"选项显示的文本:", option.text
            print u"选项值为:", option.get_attribute("value")
            option.click()
        time.sleep(1)
    def tearDown(self):
        self.driver.quit()

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

operate_select2.py
#encoding=utf-8
import unittest
import time
from selenium import webdriver

class VisitLocalWebByIE(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Ie(executable_path="D:\\IEDriverServer")
    def test_operateDropList(self):
        #自定义网页url
        url = "http://127.0.0.1:8080/test_select.html"
        #访问自定义网页
        self.driver.get(url)
        #导入select模块
        from selenium.webdriver.support.ui import Select
        #使用xpath定位方式获取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 all_options[1].is_selected():
            #方法一:通过序号选择第2个元素,序号从0开始
            select_element.select_by_index(1)
            #打印已选中项的文本
            print select_element.all_selected_options[0].text
            #断言当前选中的选项文本是否为“西瓜”
            self.assertEqual(select_element.all_selected_options[0].text,u"西瓜")
            time.sleep(2)
            #方法二:通过选项的显示文本选择文本为“猕猴桃”选项
            select_element.select_by_visible_text("猕猴桃")
            #断言先选中项的文本是否是“猕猴桃”
            self.assertEqual(select_element.all_selected_options[0].text,u"猕猴桃")
            time.sleep(2)
            #方法三:通过选项的value属性值选择value="shanzha"选项
            select_element.select_by_value("shanzha")
            print select_element.all_selected_options[0].text
            self.assertEqual(select_element.all_selected_options[0].text,u"山楂")
    def tearDown(self):
        self.driver.quit()

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

断言:比对下拉选项
assert_select.py
#encoding=utf-8
import unittest
import time
from selenium import webdriver

class VisitLocalWebByIE(unittest.TestCase):
    def setUp(self):
        #启动IE
        self.driver = webdriver.Ie(executable_path="D:\\IEDriverServer")
    def test_checkSelectText(self):
        url = "http://127.0.0.1:8080/test_select.html"
        #访问自定义html页面
        self.driver.get(url)
        #导入Select模块
        from selenium.webdriver.support.ui import Select
        #使用xpath定位方式获取select页面元素对象
        select_element = Select(self.driver.find_element_by_xpath("//select"))
        #获取所有选择项的页面元素对象
        actual_options = select_element.options
        #声明一个list,存储下拉列表中所期望出现的文字内容
        expect_optionsList = [u"桃子",u"西瓜",u"橘子",u"猕猴桃",u"山楂",u"荔枝"]
        #使用Python内置map()函数获取页面中下拉列表展示的选项内容组成的列表对象
        actual_optionsList = map(lambda option:option.text,actual_options)
        self.assertListEqual(expect_optionsList,actual_optionsList)
    def tearDown(self):
        self.driver.quit()

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

24、操作多选下拉框
使用Select模块

operate_multiple_select.py
#encoding=utf-8
import unittest
import time
from selenium import webdriver

class VisitLocalByIE(unittest.TestCase):
    def setUp(self):
        #启动浏览器
        self.driver = webdriver.Ie(executable_path="D:\\IEDriverServer")

    def test_operateMultipleOptionDropList(self):
        url = "http://127.0.0.1:8080/test_multiple_select.html"
        #访问自定义html页面
        self.driver.get(url)
        #导入Select模块
        from selenium.webdriver.support.ui import Select
        #使用xpath定位方式获取select页面元素对象
        select_element = Select(self.driver.find_element_by_xpath("//select"))
        #使用不同方法选中下拉选项
        #通过序号选择第一个选项
        select_element.select_by_index(0)
        #通过选择的文本选择“山楂”选项
        select_element.select_by_visible_text(u"山楂")
        #通过选择的value属性值选择value="mihoutao"的选项
        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(u"荔枝")
        select_element.select_by_value("juzi")
        #使用不同方法取消选中
        #通过选项文本取消已选中的文本为“荔枝”选项
        select_element.deselect_by_visible_text(u"荔枝")
        #通过序号取消已选中的序号为1的选项
        select_element.deselect_by_index(1)
        #通过选项的value属性值取消行已选中的value="juzi"的选项
        select_element.deselect_by_value("juzi")
    def tearDwon(self):
        self.driver.quit()

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

25、操作可输入的下拉框
使用Keys模块

operate_inputSelect.py
#encoding=utf-8
import unittest
import time
from selenium import webdriver

class VisitLocalWebByIE(unittest.TestCase):
    def setUp(self):
        #启动IE浏览器
        self.driver = webdriver.Ie(executable_path="D:\\IEDriverServer")
    def test_operateInputDropList(self):
        url = "http://127.0.0.1:8080/test_input_select.html"
        #访问自定义html页面
        self.driver.get(url)
        #导入Keys模块
        from selenium.webdriver.common.keys import Keys
        #通过id获取下拉框并清除
        self.driver.find_element_by_id("select").clear()
        #输入的同时按下箭头键
        self.driver.find_element_by_id("select").send_keys("c")
        self.driver.find_element_by_id("select").send_keys(Keys.ARROW_DOWN)
        self.driver.find_element_by_id("select").send_keys(Keys.ARROW_DOWN)
        self.driver.find_element_by_id("select").send_keys(Keys.ENTER)
    def tearDown(self):
        self.driver.quit()

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

26、操作单选框
#encoding=utf-8
import unittest
import time
from selenium import webdriver

class VisitLocalWebByIE(unittest.TestCase):
    def setUp(self):
        #启动IE浏览器
        self.driver = webdriver.Ie(executable_path="D:\\IEDriverServer")
    def test_Radio(self):
        url = "http://127.0.0.1:8080/test_radio.html"
        #访问自定义html网页
        self.driver.get(url)
        #使用xpath定位获取value值为“berry”的input元素对象,也就是“草莓”选项
        berryRadio = self.driver.find_element_by_xpath("//input[@value='berry']")
        #点击选择“草莓”选项
        berryRadio.click()
        #断言“草莓”单选框被成功选中
        self.assertTrue(berryRadio.is_selected(),u"草莓单选框未被选中!")
        if berryRadio.is_selected():
            #如果“草莓”单选框被成功选中,重新选择“西瓜”选项
            watermelonRadio = self.driver.find_element_by_xpath("//input[@value='watermelon']")
            watermelonRadio.click()
            #选择“西瓜”选项以后,断言“草莓”选择处于未被选中状态
            self.assertFalse(berryRadio.is_selected())
        #查找所有name属性值为fruit的单选按框元素对象,并存放在radioList列表中
        radioList = self.driver.find_elements_by_xpath("//input[@name='fruit']")
        #注意要使用find_elements_by_xpath
        print type(radioList)
        print radioList
        """
        循环遍历radioList中的每个单选按钮,查找value属性值为“orange”的单选框,
        如果找到此单选框以后,发现未处于选中状态,则调用click()方法选中该选项
        """
        for radio in radioList:
            if radio.get_attribute("value") == "orange":
                if not radio.is_selected():
                    radio.click()
                    self.assertEqual(radio.get_attribute("value"),"orange")
    def tearDown(self):
        self.driver.quit()

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

 

posted @ 2019-01-30 16:03  测试小子  阅读(243)  评论(0编辑  收藏  举报