• Selenium Webdriver API(4)


    Selenium Webdriver API(4)

    27、操作复选框 checkBox
    #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_checkBox(self):
            url = "http://127.0.0.1:8080/test_checkbox.html"
            #访问自定义网页
            self.driver.get(url)
            #使用xpath定位获取value属性为“berry”的input元素对象 ,也就是“草莓”选项
            berryCheckBox = self.driver.find_element_by_xpath("//input[@value='berry']")
            #单击选择“草莓”选项
            berryCheckBox.click()
            #断言“草莓”复选框被成功选中
            self.assertTrue(berryCheckBox.is_selected(),u"草莓复选框未被选中")
            if berryCheckBox.is_selected():
                #如果“草莓”复选框被成功选中,再次单击取消选中
                berryCheckBox.click()
                #断言“草莓”复选框处于未选中状态
                self.assertFalse(berryCheckBox.is_selected())
            #查找所有name属性值为"fruit"和复选框元素对象,并存放在checkBoxList列表中
            checkBoxList = self.driver.find_elements_by_xpath("//input[@name='fruit']")
            #遍历checkBoxList列表中所有的复选框元素,让全部复选框处于被选中状态
            for box in checkBoxList:
                if not box.is_selected():
                    box.click()
            time.sleep(4)
        def tearDown(self):
            self.driver.quit()

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


    28、断言页面源码中是否有某关键字 assert
    #encoding=utf-8
    import unittest
    import time
    from selenium import webdriver

    class VisitBaiduByIE(unittest.TestCase):
        def setUp(self):
            #启动浏览器
            self.driver = webdriver.Ie(executable_path = "D:\IEDriverServer")
        def test_assertKeyWord(self):
            url = "http://www.baidu.com"
            #访问百度首页
            self.driver.get(url)
            self.driver.find_element_by_id("kw").send_keys(u"魔兽世界")
            self.driver.find_element_by_id("su").click()
            time.sleep(4)
            #通过断言页面是否存在某些关键字来确定页面按照预期加载
            assert u"魔兽" in self.driver.page_source,u"页面源码中不存在该关键字"
        def tearDown(self):
            self.driver.quit()

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

    29、截屏 get_screenshot_as_file
    #encoding=utf-8
    import unittest
    import time
    from selenium import webdriver

    class VisitSogouByIE(unittest.TestCase):
        def setUp(self):
            #启动浏览器
            self.driver = webdriver.Ie(executable_path = "D:\IEDriverServer")
        def test_captureScreenInCurrentWindow(self):
            url = "http://www.sogou.com"
            #访问搜狗首页
            self.driver.get(url)
            try:
                """
                调用get_screenshot_as_file(filename)方法,对浏览器当前打开页面
                进行截图,并保存为screenPicture.png文件
                """
                result = self.driver.get_screenshot_as_file("D:\test\screenPicture.png")
                print result
            except IOError,e:
                print e
        def tearDown(self):
            self.driver.quit()

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

    练习:截取三个网站的首页,并以日期为文件夹名,时间为文件名保存
    #encoding=utf-8
    import time
    import os
    from selenium import webdriver
    import unittest

    class ScreenShot(unittest.TestCase):
        def setUp(self):
            #启动浏览器
            self.driver = webdriver.Ie(executable_path = "D:\IEDriverServer")
        def test_getScreenShot(self):
            urls = ["http://www.baidu.com","http://www.sogou.com","https://www.mi.com"]
            file_path = "D:\test\"+time.strftime("%Y%m%d",time.localtime())+"\"
            os.mkdir(file_path)
            for url in urls:
                self.driver.get(url)
                file_name = time.strftime("%H%M%S",time.localtime())
                try:
                    self.driver.get_screenshot_as_file(file_path+file_name+".png")
                except Exception,e:
                    print e
                time.sleep(2)
        def tearDown(self):
            self.driver.quit()

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

    30、拖拽 drag_and_drop
    #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_drag(self):
            url = "http://jqueryui.com/resources/demos/draggable/scroll.html"
            self.driver.get(url)
            #获取页面上第一个能拖拽的页面元素
            initialPosition = self.driver.find_element_by_id("draggable")
            #获取页面上第二能拖拽的页面元素
            targetPosition = self.driver.find_element_by_id("draggable2")
            #获取页面上第三个能拖拽的页面元素
            dragElement = self.driver.find_element_by_id("draggable3")
            #导入提供拖拽元素方法的模块ActionChains
            from selenium.webdriver import ActionChains
            """
            创建一个新的ActionChains,将webdriver 实例对象driver作为参数值传入,
             然后通过webdriver实例执行用户动作
            """
            action_chains = ActionChains(self.driver)
            #将页面上第一个能被拖拽的元素拖拽到第二个元素位置
            action_chains.drag_and_drop(initialPosition,targetPosition).perform()
            #将页面上第三个能被拖拽的元素,向右下拖拽10个像素,共手动5次
            for i in range(5):
                action_chains.drag_and_drop_by_offset(dragElement,10,10).perform()
                time.sleep(2)
        def tearDown(self):
            self.driver.quit()

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

  • 相关阅读:
    人工智能系统
    Maven tomcat插件配置和使用
    青春谁不糊涂3
    假设但是学习java入门,请离开SSH稍远
    Linux下top订购具体解释
    Unity3d + NGUI 多分辨率适应
    Delphi 注册文件类型 设置文件图标
    如何创建自定义的文件类型关联
    delphi 中字符串与16进制、10进制转换函数
    十六进制字符串转化成字符串输出HexToStr(Delphi版、C#版)
  • 原文地址:https://www.cnblogs.com/test-chen/p/10444120.html
Copyright © 2020-2023  润新知