zoukankan      html  css  js  c++  java
  • Python3---Selenium---Window窗口切换

    Python3---Selenium---Window窗口切换

    2020-04-10-17:29:55

      Selenium通过“handle”(窗口句柄)来定位网页窗口。窗口句柄简单说可以认为是窗口的ID。

      使用Webdriver对象的switch_to属性的 window方法,来切换窗口:

      switch_to.window(handle)

    E.G

    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome('D:chromedriver_win32chromedriver.exe')
    driver.implicitly_wait(10)
    
    driver.get('http://cdn1.python3.vip/files/selenium/sample3.html')
    
    #保存当前窗口的句柄
    mainWindow = driver.current_window_handle
    
    #点击打开新的窗口链接
    link = driver.find_element_by_tag_name('a')
    link.click()
    
    #打印出当前窗口的标题栏
    print('查看第一次标题栏:',driver.title)
    
    #切换新的窗口
    #handle 窗口句柄,window_handles属性,列表对象,里面包括了当前浏览器里面所有的窗口句柄。
    for handle in driver.window_handles:
        #切换窗口
        driver.switch_to.window(handle)
        #if 语句判断是不是我们需要的窗口
        #print(handle)
        if 'Bing' in driver.title:
            #如果是我们需要的窗口,则跳出循环
            break
    print('切换窗口标题:',driver.title)
    
    driver.switch_to.window(mainWindow)
    
    print('切换回主页面标题:',driver.title)
    time.sleep(2)
    
    
    driver.close()
  • 相关阅读:
    设计模式详解(图码)
    设计模式详解(图)
    Zookeeper学习
    取消单元格的点击事件
    ios 中生成随机数
    IOS 时间和时间戳之间转化
    偏好存空判断
    限制textfield的文字长度
    tabBar的图标不被系统渲染
    (转)IOS http请求的get 和 post的请求的区别
  • 原文地址:https://www.cnblogs.com/aaron456-rgv/p/12674817.html
Copyright © 2011-2022 走看看