zoukankan      html  css  js  c++  java
  • Selenium WebDriver-获取与切换浏览器窗口的句柄

    通过selenium webdriver去切换浏览器的窗口,需要通过句柄,具体代码如下:

    #encoding=utf-8
    import unittest
    import time
    import chardet
    from selenium import webdriver
     
    class VisitSogouByIE(unittest.TestCase):
    
        def setUp(self):
            # 启动chrome浏览器
            self.driver = webdriver.Chrome(executable_path = "e:\chromedriver")
    
        def test_operateWindowHandle(self):
            url = "http://www.baidu.com"
            self.driver.get(url)
            # 获取当前窗口句柄
            now_handle = self.driver.current_window_handle
            # 打印当前获取的窗口句柄
            print now_handle
            # 百度搜索输入框中输入“selenium”
            self.driver.find_element_by_id("kw").send_keys("w3cschool")
            # 点击搜索按钮
            self.driver.find_element_by_id("su").click()
            # 导入time包
            import time
            # 等待3秒,以便网页加载完成
            time.sleep(3)
            # 点击w3school在线教育链接
            self.driver.find_element_by_xpath('//div[@id="1"]//a[text()="w3"]').click()
            time.sleep(5)
            # 获取所有窗口句柄
            all_handles = self.driver.window_handles
            print "++++", self.driver.window_handles[-1]
            # 循环遍历所有新打开的窗口句柄,也就是说不包括主窗口
            for handle in all_handles:
                if handle != now_handle:
                    # 输出待选择的窗口句柄
                    '''
                    切换窗口,也可以用下面的方法,
                    但此种方法在selenium3.x以后官方已经不推荐使用了
                    self.driver.switch_to_window(handle)  # 老方法
                    '''
                    # 切换窗口
                    self.driver.switch_to.window(handle)  # 新方法
                    # 点击HTML5链接
                    self.driver.find_element_by_link_text('HTML5').click()
                    time.sleep(3)
                    # 关闭当前的窗口
                    self.driver.close()
            time.sleep(3)
            # 打印主窗口句柄
            print now_handle
            # 返回主窗口
            self.driver.switch_to.window(now_handle)
            time.sleep(2)
            self.driver.find_element_by_id("kw").clear()
            self.driver.find_element_by_id("kw").send_keys(u"光荣之路自动化测试培训")
            self.driver.find_element_by_id("su").click()
            time.sleep(5)
    
    
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    JS案例
    JS案例--Tab栏切换
    currentBackgroundImage:获取按钮背景图片
    笔记:UITextView内容垂直居中方法
    笔记:载入viewcontroller的几种方式
    沙盒文件的创建(简单举例)
    笔记:iOS随机数与随机数据集
    四种传值方法(通知、block、属性、NSUserDefaults)
    笔记:沙盒文件的拷贝
    笔记:iOS字符串的各种用法(字符串插入、字符串覆盖、字符串截取、分割字符串)(别人的代码直接复制过来的,我脸皮有点厚)
  • 原文地址:https://www.cnblogs.com/qingqing-919/p/8709095.html
Copyright © 2011-2022 走看看