zoukankan      html  css  js  c++  java
  • Selenium Webdriver API(7)

    Selenium Webdriver API(7)

    37、隐式等待 implicitly_wait
    #encoding=utf-8
    import unittest
    import time
    from selenium import webdriver
    from selenium.webdriver import ActionChains

    class visitSogouByIe(unittest.TestCase):
        def setUp(self):
            #启动浏览器
            self.driver = webdriver.Ie(executable_path = "D:\IEDriverServer")
        def test_implicitWiat(self):
            #导入异常类
            from selenium.common.exceptions import NoSuchElementException,TimeoutException
            #导入堆栈类
            import traceback
            url = "http://www.sogou.com"
            #访问搜狗首页
            self.driver.get(url)
            #通过driver对象的implicitly_wait()方法来设置隐式等待时间,最长等待10秒,如果10秒未找到元素示抛出异常
            self.driver.implicitly_wait(10)
            try:
                #查找sogou首页的搜索输入框页面元素
                searchBox = self.driver.find_element_by_id("query")
                #在搜索输入框中输入“魔兽世界”
                searchBox.send_keys(u"魔兽世界")
                #查找sogou首页搜索按钮页面元素
                clickButton = self.driver.find_element_by_id("stb")
                #单击搜索按钮
                clickButton.click()
            except (NoSuchElementException,TimeoutException),e:
                #打印异常的堆栈信息
                traceback.print_exc()

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

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

    38、显示等待 WebDriverWait expected_conditions 组合使用

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

    class visitLocalWebByIe(unittest.TestCase):
        def setUp(self):
            #启动浏览器
            self.driver = webdriver.Ie(executable_path = "D:\IEDriverServer")
        def test_explicitWait(self):
            #导入堆栈类
            import traceback
            #导入By类
            from selenium.webdriver.common.by import By
            #导入显示等待类
            from selenium.webdriver.support.ui import WebDriverWait
            #导入期望场景类
            from selenium.webdriver.support import expected_conditions as EC
            #导入异常类
            from selenium.common.exceptions import NoSuchElementException,TimeoutException
            url = "http://127.0.0.1:8080/test_explicity_wait.html"
            #访问自定义网站
            self.driver.get(url)
            try:
                wait = WebDriverWait(self.driver,10,0.2)
                wait.until(EC.title_is(u"你喜欢的水果"))
                print u"网页的标题是'你喜欢的水果'"
                #等待10秒,直到要找的按钮出现
                ele = WebDriverWait(self.driver,10).until(lambda x: x.find_element_by_xpath("//input[@value='Display alert box']"))
                ele.click()
                #等待alert框出现
                alert = wait.until(EC.alert_is_present())
                #打印alert框体消息
                print alert.text
                #确认警告信息
                alert.accept()
                #获取id属性值为“peach”的页面元素
                peach = self.driver.find_element_by_id("peach")
                #判断id属性值为“peach”的页面元素是否能选中
                peachElement = wait.until(EC.element_to_be_selected(peach))
                print u"下拉列表的选项'桃子'目前处于选中状态"
                #判断复选框是否可见并且能被单击
                wait.until(EC.element_to_be_clickable((By.ID,"check")))
                print u"复选框可见并且能被选中"
            except TimeoutException,e:
                #捕获超时异常
                print traceback.print_exc()
            except NoSuchElementException,e:
                #捕获NoSuchElementException异常
                print traceback.print_exc()
            except Exception,e:
                #捕获其他异常
                print traceback.print_exc()

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

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

    练习:使用显式等待,判断搜索的搜索输入框是否显示,按钮是否查点击,然后再输入关键字,再点击搜索

    #endcoding=utf-8
    import unittest
    from selenium import webdriver

    class visiTSogouByIe(unittest.TestCase):
        def setUp(self):
            #启动浏览器
            self.driver = webdriver.Ie(executable_path = "D:\IEDriverServer")
        def test_explicitWaitInSogou(self):
            #导入堆栈类
            import traceback

            #导入By类

    from selenium.webdriver.common.by import By
    #导入等待类
    from selenium.webdriver.support.ui import WebDriverWait
    #导入期望场景类
    from selenium.webdriver.support import expected_conditions as EC
    #导入异常类
    from selenium.common.exceptions import NoSuchElementException,TimeoutException
    url = "http://www.sogou.com"
    #访问搜狗
    self.driver.get(url)

            try:
                wait = WebDriverWait(self.driver,10,0.2)
                #等待10秒,检测搜索框是否在页面中
                query = wait.until(EC.visibility_of_element_located((By.ID,"query")))
                #在输入框 输入“魔兽世界”
                query.send_keys(u"魔兽世界")
                #判断搜索按钮是否可点击
                stb = wait.until(EC.element_to_be_clickable((By.ID,"stb")))
                stb.click()
                #捕获异常
            except TimeoutException,e:
                print traceback.print_exc()
            except NoSuchElementException,e:
                print traceback.print_exc()
            except Exception,e:
                print traceback.print_exc()

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

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

    39、切换浏览器窗口 句柄、title、页面源码中的关键字

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

    class visitLocalWebByIe(unittest.TestCase):
        def setUp(self):
            #启动浏览器
            self.driver = webdriver.Ie(executable_path = "D:\IEDriverServer")
        def test_switchWindowByPageSource(self):
            #导入多个异常类
            from selenium.common.exceptions import NoSuchElementException,TimeoutException
            #导入By类
            from selenium.webdriver.common.by import By
            #导入显式等待类
            from selenium.webdriver.support.ui import WebDriverWait
            #导入期望场景类
            from selenium.webdriver.support import expected_conditions as EC
            #导入堆栈类
            import traceback
            url = "http://127.0.0.1:8080/test.html"
            #访问自定义网页
            self.driver.get(url)
            #显式等待找到页面上链接方案为“sogou搜索”的链接元素,找到单击它
            WebDriverWait(self.driver,10,0.2).until(EC.element_to_be_clickable((By.LINK_TEXT,"sogou 搜索"))).click()
            #获取当前所有打开的浏览器窗口句柄
            all_handles = self.driver.window_handles
            #打印当前浏览器窗口句柄
            print self.driver.current_window_handle
            #打印打开的浏览器窗口的个数
            print len(all_handles)
            #等待2秒,以便更好地查看效果
            time.sleep(2)
            #如果存储浏览器窗口句柄的窗口不为空,再遍历all_handles中所有的浏览器句柄
            if len(all_handles) > 0:
                try:
                    for windowHandle in all_handles:
                        #切换窗口
                        self.driver.switch_to.window(windowHandle)
                        #获取当前浏览器窗口页面源代码
                        pageSource = self.driver.page_source
                        if u"搜狗搜索" in pageSource:
                            #显式等待页面搜索输入框加载完成,然后输入“sogou首页的浏览器窗口被找到”
                            WebDriverWait(self.driver,10,0.2).until(lambda x : x.find_element_by_id('query')).send_keys(u"sogou首页的浏览器窗口被找到")
                            time.sleep(2)
                except NoSuchElementException,e:
                    print traceback.print_exc()
                except TimeoutException,e:
                    print traceback.print_exc()
            #将浏览器窗口切换回默认窗口
            self.driver.switch_to.window(all_handles[0])
            #断言当前浏览器窗口页面源代码中是否包含“你爱吃水果么?”关键内容
            self.assertTrue(u"你爱吃水果么?" in self.driver.page_source)

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

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

  • 相关阅读:
    【源码学习之spark core 1.6.1 standalone模式下的作业提交】
    【源码学习之spark streaming 1.6.1 】
    spark udf 初识初用
    spark 累加历史 + 统计全部 + 行转列
    spark 都用了哪些开源东东
    kafka 官方示例代码--消费者
    104. 二叉树的最大深度
    237. 删除链表中的节点
    Leetcode-167. Two Sum II
    Leetcode-215. Kth Largest Element in an Array
  • 原文地址:https://www.cnblogs.com/test-chen/p/10535244.html
Copyright © 2011-2022 走看看