zoukankan      html  css  js  c++  java
  • Selenium WebDriver- 通过源码中的关键字找到我们要操作的句柄,用于多个窗口之间切换

    #encoding=utf-8
    import unittest
    import time
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    class VisitSogouByIE(unittest.TestCase):
    
        def setUp(self):
            #启动IE浏览器
            #self.driver = webdriver.Firefox(executable_path = "e:\geckodriver")
            self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer")
            
        def test_identifyPopUpWindowByPageSource(self):
            # 导入多个异常类型
            from selenium.common.exceptions import NoSuchWindowException,TimeoutException
            # 导入期望场景类
            from selenium.webdriver.support import expected_conditions as EC
            # 导入By类
            from selenium.webdriver.common.by import By
            # 导入WebDriverWait类
            from selenium.webdriver.support.ui import WebDriverWait
            # 导入堆栈类
            import traceback
            # 导入时间模块
            import time
            url = "http://127.0.0.1/test_popup_window.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 NoSuchWindowException, e:
                    # 如果没找到浏览器的句柄,会抛出NoSuchWindowException异常,
                    # 打印异常的堆栈信息
                    print traceback.print_exc()
                except TimeoutException, e:
                    # 显示等待超过规定时间后抛出TimeoutException异常
                    # 打印异常的堆栈信息
                    print traceback.print_exc()
            # 将浏览器窗口切换回默认窗口
            self.driver.switch_to.window(all_handles[0])
            # 断言当前浏览器窗口页面源代码中是否包含“你爱吃的水果么?”关键内容
            self.assertTrue(u"你爱吃的水果么?" in self.driver.page_source)
        
    
    
    
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    Spring-data-jpa 笔记(一)
    grpc详解 java版
    快速入门正则表达式
    异常的处理
    一位资深程序员大牛给予Java初学者的学习路线建议
    this用法
    静态代码块与非静态代码块
    类的成员变量--构造器
    Java并发机制及锁的实现原理
    JAVA内存模型
  • 原文地址:https://www.cnblogs.com/qingqing-919/p/8709639.html
Copyright © 2011-2022 走看看