zoukankan      html  css  js  c++  java
  • Selenium-多窗口处理

    弹出新的窗口,该如何处理

    1.获取当前窗口句柄

    2.元素的操作,打开新的窗口

    3.获取所有窗口句柄

    4.for循环遍历所有窗口,定位到需要操作的窗口上

      和你当前句柄不一样的就说明是新的,通过打印title来判断

    实例:打开了新的窗口

    '''
    多窗口处理
    访问火狐主页,之后再新窗口访问微博
    '''

    #! /usr/bin/env python
    #coding=utf-8
    
    from selenium import webdriver
    import time
    
    '''
    多窗口处理
    访问火狐主页,之后再新窗口访问微博
    '''
    driver = webdriver.Firefox()
    driver.get("http://i.firefoxchina.cn/?from=worldindex")
    #driver.maxmize_window()
    
    #获取当前窗口句柄
    curr_handle = driver.current_window_handle
    print("curr_handle=",curr_handle)
    time.sleep(5)
    
    #新打开的窗口
    driver.find_element_by_link_text("微 博").click()
    
    #获取所有窗口句柄
    all_handles = driver.window_handles
    
    #遍历所有窗口句柄,和你当前句柄不一样的就说明是新的,通过打印title来判断
    for h in all_handles:
        if h != curr_handle:
            #跳转到h窗口
            driver.switch_to_window(h)
            #获取到新窗口的句柄
            curr_handle = driver.current_window_handle
            print("curr_handle=",curr_handle)
            
    time.sleep(5)
    
    driver.quit()

    结果:

    curr_handle= {5a0d739b-6d2f-4130-b3fd-c8c04a7bf89b}
    curr_handle= {b7957d2c-7682-4095-85bb-2a500d472bcc}

  • 相关阅读:
    mysql批量插入数据的基类
    mount命令解析
    常用linux命令记录
    转载一篇大神的博客文章
    linux查看网卡状态
    centos7配置网卡绑定
    coentos7安装python3
    阿里云ecs 硬盘在线扩容
    centos7安装redis5
    centos7 rpm安装nginx
  • 原文地址:https://www.cnblogs.com/R-bear/p/7468635.html
Copyright © 2011-2022 走看看