zoukankan      html  css  js  c++  java
  • selenium--多窗口操作

    前戏

    想一想,我们为什么要获取窗口句柄呢?有什么用呢?

    来假设一下,我们打开了一个网站,点击了一个按钮,新打开了一个页面,我们在新页面操作完成之后,需要回到原来的页面继续操作,这时候你如果继续操作原来的页面会报错的。因为当前的窗口句柄不是原来的,这时候就需要用到driver.switch_to.window('窗口句柄')来切换到之前的窗口了

    实战

    from selenium import webdriver
    import unittest
    import time
    
    
    class Test_windowHandle(unittest.TestCase):
        def test_getwindowHandle(self):
            url = 'http://www.baidu.com'
            self.driver = webdriver.Chrome()
            self.driver.get(url)
            # 获取当前窗口句柄
            now_handle = self.driver.current_window_handle
            # 打印当前窗口句柄
            print(now_handle)
            self.driver.find_element_by_id('kw').send_keys('python')
            self.driver.find_element_by_id('su').click()
            time.sleep(3)
            self.driver.find_element_by_link_text('官网').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:
                    self.driver.switch_to.window(now_handle)  # 切换到原来的窗口
                    print(handle)
    
    
    test1 = Test_windowHandle()
    test1.test_getwindowHandle()

    结果:

    CDwindow-7A4FB6B28A26B9F733880575DE519BBB
    CDwindow-EB4CC0FE714BA702CFE7C93FEE21EF5E
    CDwindow-EB4CC0FE714BA702CFE7C93FEE21EF5E
  • 相关阅读:
    bbb SOCKET CAN
    在BBB上使用CCS5开发环境
    BBB的PRU模块
    垃圾邮件分类
    yzoj 2377 颂芬梭哈 题解
    yzoj 2372 小B的数字 题解
    yzoj P2371 爬山 题解
    hdu 1007 Quoit Design 题解
    手写堆
    yzoj P1126 塔 题解
  • 原文地址:https://www.cnblogs.com/zouzou-busy/p/11154917.html
Copyright © 2011-2022 走看看